commands.htm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <%#
  2. Copyright 2012 Jo-Philipp Wich <jow@openwrt.org>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <% css = [[
  6. .commandbox {
  7. height: 12em;
  8. width: 30%;
  9. float: left;
  10. height: 12em;
  11. margin: 5px;
  12. position: relative;
  13. }
  14. .commandbox h3 {
  15. font-size: 1.5em !important;
  16. line-height: 2em !important;
  17. margin: 0 !important;
  18. }
  19. .commandbox input[type="text"] {
  20. width: 50% !important;
  21. }
  22. .commandbox div {
  23. position: absolute;
  24. left: 0;
  25. bottom: 1.5em;
  26. }
  27. ]] -%>
  28. <%+header%>
  29. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  30. <script type="text/javascript">//<![CDATA[
  31. var stxhr = new XHR();
  32. function command_run(id)
  33. {
  34. var args;
  35. var field = document.getElementById(id);
  36. if (field)
  37. args = encodeURIComponent(field.value);
  38. var legend = document.getElementById('command-rc-legend');
  39. var output = document.getElementById('command-rc-output');
  40. if (legend && output)
  41. {
  42. output.innerHTML =
  43. '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' +
  44. '<%:Waiting for command to complete...%>'
  45. ;
  46. legend.parentNode.style.display = 'block';
  47. legend.style.display = 'inline';
  48. stxhr.get('<%=url('admin/system/commands/run')%>/' + id + (args ? '/' + args : ''), null,
  49. function(x, st)
  50. {
  51. if (st)
  52. {
  53. if (st.binary)
  54. st.stdout = '[<%:Binary data not displayed, download instead.%>]';
  55. legend.style.display = 'none';
  56. output.innerHTML = String.format(
  57. '<pre><strong># %h\n</strong>%h<span style="color:red">%h</span></pre>' +
  58. '<div class="alert-message warning">%s (<%:Code:%> %d)</div>',
  59. st.command, st.stdout, st.stderr,
  60. (st.exitcode == 0) ? '<%:Command successful%>' : '<%:Command failed%>',
  61. st.exitcode);
  62. }
  63. else
  64. {
  65. legend.style.display = 'none';
  66. output.innerHTML = '<span class="error"><%:Failed to execute command!%></span>';
  67. }
  68. location.hash = '#output';
  69. }
  70. );
  71. }
  72. }
  73. function command_download(id)
  74. {
  75. var args;
  76. var field = document.getElementById(id);
  77. if (field)
  78. args = encodeURIComponent(field.value);
  79. location.href = '<%=url('admin/system/commands/download')%>/' + id + (args ? '/' + args : '');
  80. }
  81. function command_link(id)
  82. {
  83. var legend = document.getElementById('command-rc-legend');
  84. var output = document.getElementById('command-rc-output');
  85. var args;
  86. var field = document.getElementById(id);
  87. if (field)
  88. args = encodeURIComponent(field.value);
  89. if (legend && output)
  90. {
  91. var prefix = location.protocol + '//' + location.hostname +
  92. (location.port ? ':' + location.port : '') +
  93. location.pathname.split(';')[0] + 'command/';
  94. var suffix = (args ? '/' + args : '');
  95. var link = prefix + id + suffix;
  96. var link_nodownload = prefix + id + "s" + suffix;
  97. legend.style.display = 'none';
  98. output.parentNode.style.display = 'block';
  99. output.innerHTML = String.format(
  100. '<div class="alert-message"><p><%:Download execution result%> <a href="%s">%s</a></p><p><%:Or display result%> <a href="%s">%s</a></p></div>',
  101. link, link, link_nodownload, link_nodownload
  102. );
  103. location.hash = '#output';
  104. }
  105. }
  106. //]]></script>
  107. <%
  108. local uci = require "luci.model.uci".cursor()
  109. local commands = { }
  110. uci:foreach("luci", "command", function(s) commands[#commands+1] = s end)
  111. %>
  112. <form method="get" action="<%=pcdata(luci.http.getenv("REQUEST_URI"))%>">
  113. <div class="cbi-map">
  114. <h2 name="content"><%:Custom Commands%></h2>
  115. <fieldset class="cbi-section">
  116. <% local _, command; for _, command in ipairs(commands) do %>
  117. <div class="commandbox">
  118. <h3><%=pcdata(command.name)%></h3>
  119. <p><%:Command:%> <code><%=pcdata(command.command)%></code></p>
  120. <% if command.param == "1" then %>
  121. <p><%:Arguments:%> <input type="text" id="<%=command['.name']%>" /></p>
  122. <% end %>
  123. <div>
  124. <input type="button" value="<%:Run%>" class="cbi-button cbi-button-apply" onclick="command_run('<%=command['.name']%>')" />
  125. <input type="button" value="<%:Download%>" class="cbi-button cbi-button-download" onclick="command_download('<%=command['.name']%>')" />
  126. <% if command.public == "1" then %>
  127. <input type="button" value="<%:Link%>" class="cbi-button cbi-button-link" onclick="command_link('<%=command['.name']%>')" />
  128. <% end %>
  129. </div>
  130. </div>
  131. <% end %>
  132. <br style="clear:both" /><br />
  133. <a name="output"></a>
  134. </fieldset>
  135. </div>
  136. <fieldset class="cbi-section" style="display:none">
  137. <legend id="command-rc-legend"><%:Collecting data...%></legend>
  138. <span id="command-rc-output"></span>
  139. </fieldset>
  140. </form>
  141. <%+footer%>