guest_create.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. refresh_ip_pool_id_selectpicker($('#ip_pool_id'));
  105. refresh_vlan_id_selectpicker($('#vlan_id'));
  106. refresh_project_id_selectpicker($('#project_id'));
  107. refresh_service_id_selectpicker($('#service_id'), 1);
  108. $('#bandwidth_unit').on('changed.bs.select', function (me) {
  109. render_bandwidth_slider($("#bandwidth"), 'update', $('#bandwidth_unit').val())
  110. });
  111. render_bandwidth_slider($("#bandwidth"), 'create', 'm');
  112. $('#project_id').on('changed.bs.select', function (me) {
  113. refresh_service_id_selectpicker($('#service_id'), $('#project_id').val());
  114. });
  115. });
  116. function get_os_templates_profile_mapping_by_id() {
  117. var os_templates_profile_mapping_by_id = {};
  118. $.ajax({
  119. url : '/api/os_templates_profile',
  120. type : 'GET',
  121. contentType: "application/json; charset=utf-8",
  122. dataType: 'json',
  123. async: false,
  124. error : function() {
  125. },
  126. success : function(data, textStatus, xhr) {
  127. $.each(data.data, function(k, v) {
  128. os_templates_profile_mapping_by_id[v['id']] = v;
  129. });
  130. }
  131. });
  132. return os_templates_profile_mapping_by_id;
  133. }
  134. function refresh_os_template_image_id_selectpicker(selectpicker) {
  135. $.ajax({
  136. url : '/api/os_templates_image?filter=active:eq:1',
  137. type : 'GET',
  138. contentType: "application/json; charset=utf-8",
  139. dataType: 'json',
  140. async: false,
  141. error : function() {
  142. },
  143. success : function(data, textStatus, xhr) {
  144. var os_templates_profile_mapping_by_id = get_os_templates_profile_mapping_by_id();
  145. selectpicker.empty();
  146. $.each(data.data, function(k, v) {
  147. if (v['logo'] === "") {
  148. v['logo'] = os_templates_profile_mapping_by_id[v['os_template_profile_id']].icon;
  149. }
  150. selectpicker.append(
  151. $('<option>', {value: v['id'], text: '\xa0\xa0' + v['label'], 'data-icon': v['logo']})
  152. );
  153. });
  154. selectpicker.selectpicker('refresh');
  155. }
  156. });
  157. }
  158. function refresh_ip_pool_id_selectpicker(selectpicker) {
  159. $.ajax({
  160. url : '/api/ip_pools',
  161. type : 'GET',
  162. contentType: "application/json; charset=utf-8",
  163. dataType: 'json',
  164. async: false,
  165. error : function() {
  166. },
  167. success : function(data, textStatus, xhr) {
  168. selectpicker.empty();
  169. var selected = '';
  170. $.each(data.data, function(k, v) {
  171. selectpicker.append(
  172. $('<option>', {value: v['id'], text: '\xa0\xa0' + v['name']})
  173. );
  174. if (v['activity']) {
  175. selected = v['id'];
  176. }
  177. });
  178. selectpicker.selectpicker('refresh');
  179. selectpicker.val(selected).change();
  180. }
  181. });
  182. }
  183. function refresh_vlan_id_selectpicker(selectpicker) {
  184. $.ajax({
  185. url : '/api/vlans',
  186. type : 'GET',
  187. contentType: "application/json; charset=utf-8",
  188. dataType: 'json',
  189. async: false,
  190. error : function() {
  191. },
  192. success : function(data, textStatus, xhr) {
  193. selectpicker.empty();
  194. $.each(data.data, function(k, v) {
  195. selectpicker.append(
  196. $('<option>', {value: v['vlan_id'], text: '\xa0\xa0' + v['label'], 'data-subtext': v['description']})
  197. );
  198. });
  199. selectpicker.selectpicker('refresh');
  200. selectpicker.val('-1').change();
  201. }
  202. });
  203. }
  204. function refresh_project_id_selectpicker(selectpicker) {
  205. $.ajax({
  206. url : '/api/projects',
  207. type : 'GET',
  208. contentType: "application/json; charset=utf-8",
  209. dataType: 'json',
  210. async: false,
  211. error : function() {
  212. },
  213. success : function(data, textStatus, xhr) {
  214. selectpicker.empty();
  215. $.each(data.data, function(k, v) {
  216. selectpicker.append(
  217. $('<option>', {value: v['id'], text: '\xa0\xa0' + v['name'], 'data-subtext': v['description']})
  218. );
  219. });
  220. selectpicker.selectpicker('refresh');
  221. selectpicker.val('1').change();
  222. }
  223. });
  224. }
  225. function refresh_service_id_selectpicker(selectpicker, project_id) {
  226. $.ajax({
  227. url : '/api/projects/' + project_id,
  228. type : 'GET',
  229. contentType: "application/json; charset=utf-8",
  230. dataType: 'json',
  231. async: false,
  232. error : function() {
  233. },
  234. success : function(data, textStatus, xhr) {
  235. selectpicker.empty();
  236. $.each(data.data['services'], function(k, v) {
  237. selectpicker.append(
  238. $('<option>', {value: v['id'], text: '\xa0\xa0' + v['name'], 'data-subtext': v['description']})
  239. );
  240. });
  241. selectpicker.selectpicker('refresh');
  242. if (project_id === 1) {
  243. selectpicker.val('1').change();
  244. }
  245. }
  246. });
  247. }
  248. function refresh_ssh_keys_checkboxer(checkboxer) {
  249. $.ajax({
  250. url : '/api/ssh_keys',
  251. type : 'GET',
  252. contentType: "application/json; charset=utf-8",
  253. dataType: 'json',
  254. async: false,
  255. error : function() {
  256. },
  257. success : function(data, textStatus, xhr) {
  258. checkboxer.empty();
  259. $.each(data.data, function(k, v) {
  260. checkboxer.append(
  261. '<label class="checkbox-inline"><input type="checkbox" name="ssh_keys_id" value="' + v['id'] + '">' + v['label'] + '</label>'
  262. );
  263. });
  264. }
  265. });
  266. }
  267. function create() {
  268. var ability_value = $('#create_guest_form input:radio[name="ability"]:checked').val();
  269. ability_value = ability_value.match(/^(\d+)c(\d+)g$/i);
  270. if (ability_value === null) {
  271. alter_danger('指定的配置取值有误!');
  272. return
  273. }
  274. var cpu = ability_value[1];
  275. var memory = ability_value[2];
  276. var data = {
  277. cpu: parseInt(cpu),
  278. memory: parseInt(memory),
  279. bandwidth: parseInt($('#bandwidth').val()),
  280. bandwidth_unit: $('#bandwidth_unit').val(),
  281. os_template_image_id: parseInt($('#os_template_image_id').val()),
  282. quantity: parseInt($('#quantity').val()),
  283. remark: $('#remark').val(),
  284. password: $('#password').val(),
  285. ssh_keys_id: $("input[name=ssh_keys_id]:checked").map(function () {return this.value}).get(),
  286. lease_term: 100
  287. };
  288. if (!$('#allocation_by_random').prop('checked')) {
  289. data['node_id'] = $('#node_id').val()
  290. }
  291. var go_back_url = '/guests';
  292. $.ajax({
  293. url : '/api/guest',
  294. type : 'POST',
  295. contentType: "application/json; charset=utf-8",
  296. async: false,
  297. dataType: "json",
  298. data : JSON.stringify(data),
  299. error : function(data, textStatus, xhr) {
  300. alter_warning('创建实例失败!');
  301. alter_danger(data.responseText);
  302. },
  303. success : function(data, textStatus, xhr) {
  304. alter_success('您所提交的资源正在创建中。根据所提交资源的大小,需要等待几到十几分钟。页面将在3秒钟后自动跳转到实例列表页面!');
  305. $.ajax({
  306. url: '/api/guests',
  307. type : 'GET',
  308. dataType: "json",
  309. error : function(data, textStatus, xhr) {
  310. },
  311. success : function(data, textStatus, xhr) {
  312. var page_size = 20;
  313. var last_page = Math.ceil(data['paging']['total'] / page_size);
  314. go_back_url = "/guests?page_size=" + page_size + "&page=" + last_page;
  315. }
  316. });
  317. }
  318. });
  319. setTimeout(function() {
  320. window.location.href=go_back_url;
  321. }, 3000);
  322. }
  323. $(function() { "use strict";
  324. $("#quantity").TouchSpin({
  325. max: 20,
  326. min: 1,
  327. verticalbuttons: true,
  328. verticalupclass: 'glyph-icon icon-plus',
  329. verticaldownclass: 'glyph-icon icon-minus'
  330. });
  331. });
  332. function allocation_by_random_click_handler(me) {
  333. if ($(me).prop('checked')) {
  334. $('#create_guest_form').formValidation('removeField', 'node_id');
  335. $('#node_id').parent().parent().css('display', 'none');
  336. } else {
  337. $('#create_guest_form').formValidation('addField', 'node_id', {validators: {notEmpty: {}}});
  338. $('#node_id').parent().parent().css('display', 'unset');
  339. }
  340. }
  341. </script>
  342. <div id="page-content-wrapper">
  343. <div id="alert-success-tip" class="alert alert-success alert-top">
  344. </div>
  345. <div id="alert-warning-tip" class="alert alert-warning alert-top">
  346. </div>
  347. <div id="alert-danger-tip" class="alert alert-danger alert-top">
  348. </div>
  349. </div>
  350. <div class="container" style="padding-top: 100px;">
  351. <div class="panel">
  352. <div class="panel-body">
  353. <h3 class="title-hero" style="display: inline;">
  354. 创建实例
  355. </h3>
  356. <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;">
  357. <span class="glyph-icon icon-separator" style="transform: rotateY(-180deg);">
  358. <i class="glyph-icon icon-level-up"></i>
  359. </span>
  360. <span class="button-content">
  361. 返回虚拟机列表
  362. </span>
  363. </a>
  364. <div class="example-box-wrapper">
  365. <form id="create_guest_form" class="form-horizontal bordered-row" action="javascript:;">
  366. <div class="form-group">
  367. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-gauge"></span>&nbsp;&nbsp;运算能力</label>
  368. <div class="col-sm-10">
  369. <div class="btn-ability-line" data-toggle="buttons">
  370. <label class="btn btn-default btn-ability">
  371. <input title="1 CPU 1GB Memory" name="ability" type="radio" value="1c1g">1&nbsp;&nbsp;核&nbsp;&nbsp;1&nbsp;&nbsp;GB
  372. </label>
  373. <label class="btn btn-default btn-ability">
  374. <input title="1 CPU 2GB Memory" name="ability" type="radio" value="1c2g">1&nbsp;&nbsp;核&nbsp;&nbsp;2&nbsp;&nbsp;GB
  375. </label>
  376. <label class="btn btn-default btn-ability">
  377. <input title="2 CPU 2GB Memory" name="ability" type="radio" value="2c2g">2&nbsp;&nbsp;核&nbsp;&nbsp;2&nbsp;&nbsp;GB
  378. </label>
  379. <label class="btn btn-default btn-ability">
  380. <input title="2 CPU 4GB Memory" name="ability" type="radio" value="2c4g">2&nbsp;&nbsp;核&nbsp;&nbsp;4&nbsp;&nbsp;GB
  381. </label>
  382. <label class="btn btn-default btn-ability">
  383. <input title="4 CPU 4GB Memory" name="ability" type="radio" value="4c4g">4&nbsp;&nbsp;核&nbsp;&nbsp;4&nbsp;&nbsp;GB
  384. </label>
  385. <label class="btn btn-default btn-ability">
  386. <input title="4 CPU 8GB Memory" name="ability" type="radio" value="4c8g">4&nbsp;&nbsp;核&nbsp;&nbsp;8&nbsp;&nbsp;GB
  387. </label>
  388. <label class="btn btn-default btn-ability">
  389. <input title="8 CPU 8GB Memory" name="ability" type="radio" value="8c8g">8&nbsp;&nbsp;核&nbsp;&nbsp;8&nbsp;&nbsp;GB
  390. </label>
  391. <label class="btn btn-default btn-ability">
  392. <input title="8 CPU 16GB Memory" name="ability" type="radio" value="8c16g">8&nbsp;&nbsp;核&nbsp;&nbsp;16GB
  393. </label>
  394. <label class="btn btn-default btn-ability">
  395. <input title="1 CPU 16GB Memory" name="ability" type="radio" value="1c16g">1&nbsp;&nbsp;核&nbsp;&nbsp;16GB
  396. </label>
  397. <label class="btn btn-default btn-ability">
  398. <input title="4 CPU 32GB Memory" name="ability" type="radio" value="4c32g">4&nbsp;&nbsp;核&nbsp;&nbsp;32GB
  399. </label>
  400. <label class="btn btn-default btn-ability">
  401. <input title="8 CPU 32GB Memory" name="ability" type="radio" value="8c32g">8&nbsp;&nbsp;核&nbsp;&nbsp;32GB
  402. </label>
  403. <label class="btn btn-default btn-ability">
  404. <input title="8 CPU 64GB Memory" name="ability" type="radio" value="8c64g">8&nbsp;&nbsp;核&nbsp;&nbsp;64GB
  405. </label>
  406. <label class="btn btn-default btn-ability">
  407. <input title="16 CPU 16GB Memory" name="ability" type="radio" value="16c16g">16核&nbsp;&nbsp;16GB
  408. </label>
  409. <label class="btn btn-default btn-ability">
  410. <input title="24 CPU 24GB Memory" name="ability" type="radio" value="24c24g">24核&nbsp;&nbsp;24GB
  411. </label>
  412. <label class="btn btn-default btn-ability">
  413. <input title="32 CPU 32GB Memory" name="ability" type="radio" value="32c32g">32核&nbsp;&nbsp;32GB
  414. </label>
  415. <label class="btn btn-default btn-ability">
  416. <input title="64 CPU 64GB Memory" name="ability" type="radio" value="64c64g">64核&nbsp;&nbsp;64GB
  417. </label>
  418. </div>
  419. </div>
  420. </div>
  421. <div class="form-group">
  422. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-inbox-alt"></span>&nbsp;&nbsp;系统模板</label>
  423. <div class="col-sm-6">
  424. <select id="os_template_image_id" name="os_template_image_id" title="请选择系统模板..." class="selectpicker" data-width="100%">
  425. </select>
  426. </div>
  427. </div>
  428. <div class="form-group">
  429. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-th-list"></span>&nbsp;&nbsp;数量</label>
  430. <div class="col-sm-6">
  431. <input id="quantity" title="实例数量" class="form-control" type="text" value="1" name="quantity">
  432. </div>
  433. </div>
  434. <div class="form-group">
  435. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-compass-circled"></span>&nbsp;&nbsp;标识&密码</label>
  436. <div class="col-sm-6">
  437. <div class="row form-group">
  438. <label class="col-sm-2 control-label">登录密码:</label>
  439. <div class="col-sm-6">
  440. <input type="password" name="password" title="实例密码" class="form-control" id="password" placeholder="如不填写,系统自动生成16位随机密码">
  441. </div>
  442. </div>
  443. <div class="row form-group">
  444. <label class="col-sm-2 control-label">确认密码:</label>
  445. <div class="col-sm-6">
  446. <input type="password" name="confirm_password" title="确认实例密码" class="form-control" id="confirm_password">
  447. </div>
  448. </div>
  449. <div style="height: 10px;"></div>
  450. <div class="row form-group">
  451. <label class="col-sm-2 control-label">实例名称:</label>
  452. <div class="col-sm-6">
  453. <input id="remark" name="remark" type="text" title="实例名称" class="form-control" placeholder="如不填写,系统自动默认生成">
  454. </div>
  455. </div>
  456. </div>
  457. </div>
  458. <div class="form-group">
  459. <label class="col-sm-2 control-label"><span class="glyph-icon icon-signal"></span>&nbsp;&nbsp;带宽</label>
  460. <div class="col-sm-7">
  461. <input id="bandwidth" title="带宽" type="text" name="bandwidth">
  462. </div>
  463. <div class="col-sm-1" style="bottom: -10px;">
  464. <select id="bandwidth_unit" name="bandwidth_unit" title="单位..." class="selectpicker" data-width="120%">
  465. <option value="k">&nbsp;&nbsp;Kbps</option>
  466. <option value="m" selected>&nbsp;&nbsp;Mbps</option>
  467. <option value="g">&nbsp;&nbsp;Gbps</option>
  468. </select>
  469. </div>
  470. </div>
  471. <div class="form-group">
  472. <label class="col-sm-2 control-label"><span class="glyph-icon icon-key"></span>&nbsp;&nbsp;SSH-KEY</label>
  473. <div class="col-sm-6" id="ssh_keys_id_div">
  474. </div>
  475. </div>
  476. <div class="form-group" style="text-align: center;">
  477. <a href="javascript:;">高级&nbsp;&nbsp;&nbsp;&nbsp;<span class="glyph-icon icon-caret-down"></span></a>
  478. </div>
  479. <div class="form-group">
  480. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-shuffle"></span>&nbsp;&nbsp;创建模式</label>
  481. <div class="col-sm-6">
  482. <div class="row form-group">
  483. <label class="col-sm-2 control-label">随机分配:</label>
  484. <div class="col-sm-6">
  485. <input onclick="allocation_by_random_click_handler(this);" type="checkbox" name="allocation_by_random" title="随机分配" class="form-control" id="allocation_by_random" checked>
  486. </div>
  487. </div>
  488. <div class="row form-group" style="display: none;">
  489. <select id="node_id" name="node_id" title="请选择计算节点..." class="selectpicker" data-width="100%">
  490. {% for item in hosts_ret.data %}
  491. <option value="{{ item.node_id }}" data-icon="{{ item.icon }}">&nbsp;&nbsp;{{ item.hostname }}</option>
  492. {% endfor %}
  493. </select>
  494. </div>
  495. </div>
  496. </div>
  497. <div class="form-group">
  498. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-inbox-alt"></span>&nbsp;&nbsp;IP 池</label>
  499. <div class="col-sm-6">
  500. <select id="ip_pool_id" name="ip_pool_id" title="请选 IP 池..." class="selectpicker" data-width="100%">
  501. </select>
  502. </div>
  503. </div>
  504. <div class="form-group">
  505. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-inbox-alt"></span>&nbsp;&nbsp;VLAN</label>
  506. <div class="col-sm-6">
  507. <select id="vlan_id" name="vlan_id" title="请选所属网络..." class="selectpicker" data-width="100%">
  508. </select>
  509. </div>
  510. </div>
  511. <div class="form-group">
  512. <label class="col-sm-2 control-label"><span class="glyph-icon icon-elusive-inbox-alt"></span>&nbsp;&nbsp;项目编制</label>
  513. <div class="col-sm-3">
  514. <select id="project_id" name="project_id" title="请选所属项目..." class="selectpicker" data-width="100%">
  515. </select>
  516. </div>
  517. <div class="col-sm-3">
  518. <select id="service_id" name="service_id" title="请选所属服务..." class="selectpicker" data-width="100%">
  519. </select>
  520. </div>
  521. </div>
  522. <div class="form-group">
  523. <label class="col-sm-2 control-label"></label>
  524. <div class="col-sm-3 pull-right">
  525. <button id="create_guest_form_button" type="submit" class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;" disabled>创建</button>
  526. </div>
  527. </div>
  528. </form>
  529. </div>
  530. </div>
  531. </div>
  532. </div>
  533. {% endblock body %}