guest_create.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. {% extends theme("layout.html") %}
  2. {% block head %}
  3. {{ super() }}
  4. <style type="text/css">
  5. @media (min-width: 768px) {
  6. .form-horizontal .control-label {
  7. text-align: left;
  8. }
  9. }
  10. label>span {
  11. color: deepskyblue;
  12. }
  13. .btn,
  14. .form-group>div>div,
  15. .form-control {
  16. border-radius: 0;
  17. }
  18. .btn-ability {
  19. margin-right: 16px;
  20. height: 50px;
  21. padding: 8px 50px;
  22. margin-bottom: 30px;
  23. }
  24. .btn-ability-line {
  25. }
  26. </style>
  27. {% endblock head %}
  28. {% block body %}
  29. <script>
  30. $(document).ready(function() {
  31. $('body').addClass('add-transition');
  32. $('.add-page-transition').on('click', function(){
  33. var transAttr = $(this).attr('data-transition');
  34. $('.add-transition').attr('class', 'add-transition');
  35. $('.add-transition').addClass(transAttr);
  36. });
  37. $('#create_guest_form').formValidation({
  38. framework: 'bootstrap4',
  39. icon: {
  40. valid: 'fa fa-check',
  41. invalid: 'fa fa-times',
  42. validating: 'fa fa-refresh'
  43. },
  44. // Since the Bootstrap Button hides the radio and checkbox
  45. // We exclude the disabled elements only
  46. excluded: ':disabled',
  47. locale: 'zh_CN',
  48. fields: {
  49. ability: {
  50. validators: {
  51. notEmpty: {
  52. }
  53. }
  54. },
  55. os_template_image_id: {
  56. validators: {
  57. notEmpty: {
  58. }
  59. }
  60. },
  61. quantity: {
  62. validators: {
  63. between: {
  64. min: 1,
  65. max: 20
  66. }
  67. }
  68. },
  69. password: {
  70. validators: {
  71. identical: {
  72. field: 'confirm_password'
  73. },
  74. stringLength: {
  75. max: 128
  76. }
  77. }
  78. },
  79. confirm_password: {
  80. validators: {
  81. identical: {
  82. field: 'password'
  83. }
  84. }
  85. },
  86. remark: {
  87. validators: {
  88. stringLength: {
  89. max: 64
  90. }
  91. }
  92. }
  93. }
  94. })
  95. .on('success.field.fv', function(e, data) {
  96. if (data.fv.getInvalidFields().length > 0) { // There is invalid field
  97. data.fv.disableSubmitButtons(true);
  98. }
  99. }).on('success.form.fv', function (e, data) {
  100. create();
  101. });
  102. refresh_os_template_image_id_selectpicker($('#os_template_image_id'));
  103. refresh_ssh_keys_checkboxer($('#ssh_keys_id_div'));
  104. $('#bandwidth_unit').on('changed.bs.select', function (me) {
  105. render_bandwidth_slider($("#bandwidth"), 'update', $('#bandwidth_unit').val())
  106. });
  107. render_bandwidth_slider($("#bandwidth"), 'create', 'm');
  108. });
  109. function get_os_templates_profile_mapping_by_id() {
  110. var os_templates_profile_mapping_by_id = {};
  111. $.ajax({
  112. url : '/api/os_templates_profile',
  113. type : 'GET',
  114. contentType: "application/json; charset=utf-8",
  115. dataType: 'json',
  116. async: false,
  117. error : function() {
  118. },
  119. success : function(data, textStatus, xhr) {
  120. $.each(data.data, function(k, v) {
  121. os_templates_profile_mapping_by_id[v['id']] = v;
  122. });
  123. }
  124. });
  125. return os_templates_profile_mapping_by_id;
  126. }
  127. function refresh_os_template_image_id_selectpicker(selectpicker) {
  128. $.ajax({
  129. url : '/api/os_templates_image?filter=active:eq:1',
  130. type : 'GET',
  131. contentType: "application/json; charset=utf-8",
  132. dataType: 'json',
  133. async: false,
  134. error : function() {
  135. },
  136. success : function(data, textStatus, xhr) {
  137. var os_templates_profile_mapping_by_id = get_os_templates_profile_mapping_by_id();
  138. selectpicker.empty();
  139. $.each(data.data, function(k, v) {
  140. if (v['logo'] === "") {
  141. v['logo'] = os_templates_profile_mapping_by_id[v['os_template_profile_id']].icon;
  142. }
  143. selectpicker.append(
  144. $('<option>', {value: v['id'], text: '\xa0\xa0' + v['label'], 'data-icon': v['logo']})
  145. );
  146. });
  147. selectpicker.selectpicker('refresh');
  148. }
  149. });
  150. }
  151. function refresh_ssh_keys_checkboxer(checkboxer) {
  152. $.ajax({
  153. url : '/api/ssh_keys',
  154. type : 'GET',
  155. contentType: "application/json; charset=utf-8",
  156. dataType: 'json',
  157. async: false,
  158. error : function() {
  159. },
  160. success : function(data, textStatus, xhr) {
  161. checkboxer.empty();
  162. $.each(data.data, function(k, v) {
  163. checkboxer.append(
  164. '<label class="checkbox-inline"><input type="checkbox" name="ssh_keys_id" value="' + v['id'] + '">' + v['label'] + '</label>'
  165. );
  166. });
  167. }
  168. });
  169. }
  170. function create() {
  171. var ability_value = $('#create_guest_form input:radio[name="ability"]:checked').val();
  172. ability_value = ability_value.match(/^(\d+)c(\d+)g$/i);
  173. if (ability_value === null) {
  174. alter_danger('指定的配置取值有误!');
  175. return
  176. }
  177. var cpu = ability_value[1];
  178. var memory = ability_value[2];
  179. var data = {
  180. cpu: parseInt(cpu),
  181. memory: parseInt(memory),
  182. bandwidth: parseInt($('#bandwidth').val()),
  183. bandwidth_unit: $('#bandwidth_unit').val(),
  184. os_template_image_id: parseInt($('#os_template_image_id').val()),
  185. quantity: parseInt($('#quantity').val()),
  186. remark: $('#remark').val(),
  187. password: $('#password').val(),
  188. ssh_keys_id: $("input[name=ssh_keys_id]:checked").map(function () {return this.value}).get(),
  189. lease_term: 100
  190. };
  191. if (!$('#allocation_by_random').prop('checked')) {
  192. data['node_id'] = $('#node_id').val()
  193. }
  194. var go_back_url = '/guests';
  195. $.ajax({
  196. url : '/api/guest',
  197. type : 'POST',
  198. contentType: "application/json; charset=utf-8",
  199. async: false,
  200. dataType: "json",
  201. data : JSON.stringify(data),
  202. error : function(data, textStatus, xhr) {
  203. alter_warning('创建实例失败!');
  204. alter_danger(data.responseText);
  205. },
  206. success : function(data, textStatus, xhr) {
  207. alter_success('您所提交的资源正在创建中。根据所提交资源的大小,需要等待几到十几分钟。页面将在3秒钟后自动跳转到实例列表页面!');
  208. $.ajax({
  209. url: '/api/guests',
  210. type : 'GET',
  211. dataType: "json",
  212. error : function(data, textStatus, xhr) {
  213. },
  214. success : function(data, textStatus, xhr) {
  215. var page_size = 10;
  216. var last_page = Math.ceil(data['paging']['total'] / page_size);
  217. go_back_url = "/guests?page_size=" + page_size + "&page=" + last_page;
  218. }
  219. });
  220. }
  221. });
  222. setTimeout(function() {
  223. window.location.href=go_back_url;
  224. }, 3000);
  225. }
  226. $(function() { "use strict";
  227. $("#quantity").TouchSpin({
  228. max: 20,
  229. min: 1,
  230. verticalbuttons: true,
  231. verticalupclass: 'glyph-icon icon-plus',
  232. verticaldownclass: 'glyph-icon icon-minus'
  233. });
  234. });
  235. function allocation_by_random_click_handler(me) {
  236. if ($(me).prop('checked')) {
  237. $('#create_guest_form').formValidation('removeField', 'node_id');
  238. $('#node_id').parent().parent().css('display', 'none');
  239. } else {
  240. $('#create_guest_form').formValidation('addField', 'node_id', {validators: {notEmpty: {}}});
  241. $('#node_id').parent().parent().css('display', 'unset');
  242. }
  243. }
  244. </script>
  245. <div id="page-content-wrapper">
  246. <div id="alert-success-tip" class="alert alert-success alert-top">
  247. </div>
  248. <div id="alert-warning-tip" class="alert alert-warning alert-top">
  249. </div>
  250. <div id="alert-danger-tip" class="alert alert-danger alert-top">
  251. </div>
  252. </div>
  253. <div class="container" style="padding-top: 100px;">
  254. <div class="panel">
  255. <div class="panel-body">
  256. <h3 class="title-hero" style="display: inline;">
  257. 创建实例
  258. </h3>
  259. <a href="/guests" class="btn btn-xs btn-default add-page-transition" data-transition="pt-page-moveFromLeft-init" style="margin-bottom: 4px; margin-left: 10px;">
  260. <span class="glyph-icon icon-separator" style="transform: rotateY(-180deg);">
  261. <i class="glyph-icon icon-level-up"></i>
  262. </span>
  263. <span class="button-content">
  264. 返回虚拟机列表
  265. </span>
  266. </a>
  267. <div class="example-box-wrapper">
  268. <form id="create_guest_form" class="form-horizontal bordered-row" action="javascript:;">
  269. <div class="form-group">
  270. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-gauge"></span>&nbsp;&nbsp;运算能力</label>
  271. <div class="col-sm-10">
  272. <div class="btn-ability-line" data-toggle="buttons">
  273. <label class="btn btn-default btn-ability">
  274. <input title="1 CPU 1GB Memory" name="ability" type="radio" value="1c1g">1&nbsp;&nbsp;核&nbsp;&nbsp;1&nbsp;&nbsp;GB
  275. </label>
  276. <label class="btn btn-default btn-ability">
  277. <input title="1 CPU 2GB Memory" name="ability" type="radio" value="1c2g">1&nbsp;&nbsp;核&nbsp;&nbsp;2&nbsp;&nbsp;GB
  278. </label>
  279. <label class="btn btn-default btn-ability">
  280. <input title="2 CPU 2GB Memory" name="ability" type="radio" value="2c2g">2&nbsp;&nbsp;核&nbsp;&nbsp;2&nbsp;&nbsp;GB
  281. </label>
  282. <label class="btn btn-default btn-ability">
  283. <input title="2 CPU 4GB Memory" name="ability" type="radio" value="2c4g">2&nbsp;&nbsp;核&nbsp;&nbsp;4&nbsp;&nbsp;GB
  284. </label>
  285. <label class="btn btn-default btn-ability">
  286. <input title="4 CPU 4GB Memory" name="ability" type="radio" value="4c4g">4&nbsp;&nbsp;核&nbsp;&nbsp;4&nbsp;&nbsp;GB
  287. </label>
  288. <label class="btn btn-default btn-ability">
  289. <input title="4 CPU 8GB Memory" name="ability" type="radio" value="4c8g">4&nbsp;&nbsp;核&nbsp;&nbsp;8&nbsp;&nbsp;GB
  290. </label>
  291. <label class="btn btn-default btn-ability">
  292. <input title="8 CPU 8GB Memory" name="ability" type="radio" value="8c8g">8&nbsp;&nbsp;核&nbsp;&nbsp;8&nbsp;&nbsp;GB
  293. </label>
  294. <label class="btn btn-default btn-ability">
  295. <input title="8 CPU 16GB Memory" name="ability" type="radio" value="8c16g">8&nbsp;&nbsp;核&nbsp;&nbsp;16GB
  296. </label>
  297. <label class="btn btn-default btn-ability">
  298. <input title="1 CPU 16GB Memory" name="ability" type="radio" value="1c16g">1&nbsp;&nbsp;核&nbsp;&nbsp;16GB
  299. </label>
  300. <label class="btn btn-default btn-ability">
  301. <input title="4 CPU 32GB Memory" name="ability" type="radio" value="4c32g">4&nbsp;&nbsp;核&nbsp;&nbsp;32GB
  302. </label>
  303. <label class="btn btn-default btn-ability">
  304. <input title="4 CPU 64GB Memory" name="ability" type="radio" value="4c64g">4&nbsp;&nbsp;核&nbsp;&nbsp;64GB
  305. </label>
  306. <label class="btn btn-default btn-ability">
  307. <input title="8 CPU 64GB Memory" name="ability" type="radio" value="8c64g">8&nbsp;&nbsp;核&nbsp;&nbsp;64GB
  308. </label>
  309. <label class="btn btn-default btn-ability">
  310. <input title="16 CPU 16GB Memory" name="ability" type="radio" value="16c16g">16核&nbsp;&nbsp;16GB
  311. </label>
  312. <label class="btn btn-default btn-ability">
  313. <input title="24 CPU 24GB Memory" name="ability" type="radio" value="24c24g">24核&nbsp;&nbsp;24GB
  314. </label>
  315. <label class="btn btn-default btn-ability">
  316. <input title="32 CPU 32GB Memory" name="ability" type="radio" value="32c32g">32核&nbsp;&nbsp;32GB
  317. </label>
  318. <label class="btn btn-default btn-ability">
  319. <input title="64 CPU 64GB Memory" name="ability" type="radio" value="64c64g">64核&nbsp;&nbsp;64GB
  320. </label>
  321. </div>
  322. </div>
  323. </div>
  324. <div class="form-group">
  325. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-inbox-alt"></span>&nbsp;&nbsp;系统模板</label>
  326. <div class="col-sm-6">
  327. <select id="os_template_image_id" name="os_template_image_id" title="请选择系统模板..." class="selectpicker" data-width="100%">
  328. </select>
  329. </div>
  330. </div>
  331. <div class="form-group">
  332. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-th-list"></span>&nbsp;&nbsp;数量</label>
  333. <div class="col-sm-6">
  334. <input id="quantity" title="实例数量" class="form-control" type="text" value="1" name="quantity">
  335. </div>
  336. </div>
  337. <div class="form-group">
  338. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-compass-circled"></span>&nbsp;&nbsp;标识&密码</label>
  339. <div class="col-sm-6">
  340. <div class="row form-group">
  341. <label class="col-sm-2 control-label">登录密码:</label>
  342. <div class="col-sm-6">
  343. <input type="password" name="password" title="实例密码" class="form-control" id="password" placeholder="如不填写,系统自动生成16位随机密码">
  344. </div>
  345. </div>
  346. <div class="row form-group">
  347. <label class="col-sm-2 control-label">确认密码:</label>
  348. <div class="col-sm-6">
  349. <input type="password" name="confirm_password" title="确认实例密码" class="form-control" id="confirm_password">
  350. </div>
  351. </div>
  352. <div style="height: 10px;"></div>
  353. <div class="row form-group">
  354. <label class="col-sm-2 control-label">实例名称:</label>
  355. <div class="col-sm-6">
  356. <input id="remark" name="remark" type="text" title="实例名称" class="form-control" placeholder="如不填写,系统自动默认生成">
  357. </div>
  358. </div>
  359. </div>
  360. </div>
  361. <div class="form-group">
  362. <label class="col-sm-2 control-label"><span class="glyph-icon icon-signal"></span>&nbsp;&nbsp;带宽</label>
  363. <div class="col-sm-7">
  364. <input id="bandwidth" title="带宽" type="text" name="bandwidth">
  365. </div>
  366. <div class="col-sm-1" style="bottom: -10px;">
  367. <select id="bandwidth_unit" name="bandwidth_unit" title="单位..." class="selectpicker" data-width="120%">
  368. <option value="k">&nbsp;&nbsp;Kbps</option>
  369. <option value="m" selected>&nbsp;&nbsp;Mbps</option>
  370. <option value="g">&nbsp;&nbsp;Gbps</option>
  371. </select>
  372. </div>
  373. </div>
  374. <div class="form-group">
  375. <label class="col-sm-2 control-label"><span class="glyph-icon icon-key"></span>&nbsp;&nbsp;SSH-KEY</label>
  376. <div class="col-sm-6" id="ssh_keys_id_div">
  377. </div>
  378. </div>
  379. <div class="form-group">
  380. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-shuffle"></span>&nbsp;&nbsp;创建模式</label>
  381. <div class="col-sm-6">
  382. <div class="row form-group">
  383. <label class="col-sm-2 control-label">随机分配:</label>
  384. <div class="col-sm-6">
  385. <input onclick="allocation_by_random_click_handler(this);" type="checkbox" name="allocation_by_random" title="随机分配" class="form-control" id="allocation_by_random" checked>
  386. </div>
  387. </div>
  388. <div class="row form-group" style="display: none;">
  389. <select id="node_id" name="node_id" title="请选择计算节点..." class="selectpicker" data-width="100%">
  390. {% for item in hosts_ret.data %}
  391. <option value="{{ item.node_id }}" data-icon="{{ item.icon }}">&nbsp;&nbsp;{{ item.hostname }}</option>
  392. {% endfor %}
  393. </select>
  394. </div>
  395. </div>
  396. </div>
  397. <div class="form-group">
  398. <label class="col-sm-2 control-label"></label>
  399. <div class="col-sm-3 pull-right">
  400. <button id="create_guest_form_button" type="submit" class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;" disabled>创建</button>
  401. </div>
  402. </div>
  403. </form>
  404. </div>
  405. </div>
  406. </div>
  407. </div>
  408. {% endblock body %}