From d0858193edb96b99f88d79f9b8540ada0271003d Mon Sep 17 00:00:00 2001 From: BjoernDaase Date: Wed, 6 Feb 2019 13:09:04 +0100 Subject: [PATCH] Revert "Update Masterbranch before Demo (#375)" (#376) This reverts commit d69bf25c7ca2428c90c9673edb5376cb77fee18c. --- .gitignore | 8 +- .travis.yml | 3 - Gemfile | 3 - Gemfile.lock | 214 ++++++++-------- app/api/v_sphere/folder.rb | 12 +- app/api/v_sphere/host.rb | 19 -- app/api/v_sphere/virtual_machine.rb | 185 +++----------- app/assets/javascripts/application.js | 5 +- .../javascripts/prevent_anchor_reload.js | 50 ---- app/assets/javascripts/request.js | 16 +- app/assets/stylesheets/layout.scss | 8 +- app/assets/stylesheets/requests.scss | 11 - app/assets/stylesheets/vms.scss | 5 - app/controllers/application_controller.rb | 4 +- app/controllers/dashboard_controller.rb | 2 +- app/controllers/hosts_controller.rb | 3 +- app/controllers/projects_controller.rb | 5 - app/controllers/requests_controller.rb | 123 ++-------- app/controllers/users_controller.rb | 9 +- app/controllers/vms_controller.rb | 62 ++--- app/helpers/operating_systems_helper.rb | 2 +- app/helpers/requests_helper.rb | 11 +- app/helpers/vms_helper.rb | 13 - app/jobs/application_job.rb | 8 - app/mailers/application_mailer.rb | 2 +- app/mailers/notification_mailer.rb | 12 - app/models/archivation_request.rb | 2 +- app/models/request.rb | 89 +------ app/models/request_template.rb | 10 +- app/models/user.rb | 54 +---- app/models/virtual_machine_config.rb | 1 - app/views/application/_navbar.html.erb | 8 +- app/views/dashboard/index.html.erb | 14 +- app/views/devise/sessions/new.html.erb | 60 ++--- app/views/errors/timeout.html.erb | 3 +- app/views/hosts/show.html.erb | 53 ++-- .../notification_mailer/notify_email.html.erb | 11 - .../notification_mailer/notify_email.text.erb | 1 - app/views/request_templates/_form.html.erb | 4 +- app/views/requests/_form.html.erb | 189 +++++---------- app/views/requests/_request.json.jbuilder | 2 +- app/views/requests/index.html.erb | 149 ++++-------- app/views/requests/new.html.erb | 25 +- app/views/requests/show.html.erb | 33 +-- app/views/servers/_form.html.erb | 26 +- app/views/servers/show.html.erb | 6 +- .../templates/_resource_allocation.html.erb | 6 +- app/views/users/index.html.erb | 7 - app/views/users/show.html.erb | 16 -- app/views/vms/edit.html.erb | 30 +-- app/views/vms/edit_config.html.erb | 16 +- app/views/vms/index.html.erb | 63 ++--- app/views/vms/show.html.erb | 44 ++-- config/application.rb | 3 - config/deploy.rb | 2 +- config/environment_variables.example.yml | 19 -- config/environments/development.rb | 17 +- config/environments/production.rb | 15 +- config/initializers/environment_variables.rb | 15 -- config/routes.rb | 34 ++- ...e_join_table_requests_responsible_users.rb | 9 - ...190121210719_create_archivation_request.rb | 2 - ...23102046_create_virtual_machine_configs.rb | 2 - ...9184200_add_email_notifications_to_user.rb | 7 - ..._description_to_virtual_machine_configs.rb | 7 - .../20190131115425_rename_request_mb_to_gb.rb | 23 -- ...join_table_virtual_machine_config_users.rb | 10 - ...20190204164433_add_projects_to_requests.rb | 9 - ...5115620_add_port_forwarding_to_requests.rb | 7 - db/schema.rb | 18 +- db/seeds.rb | 6 + lib/git_helper.rb | 64 ----- lib/puppetscript.rb | 103 -------- spec/api/apiwrapper_spec.rb | 16 +- spec/api/v_sphere_api_mocker.rb | 87 +------ spec/api/v_sphere_api_spec.rb | 41 +--- spec/controllers/hosts_controller_spec.rb | 95 +++----- .../request_templates_controller_spec.rb | 1 - spec/controllers/requests_controller_spec.rb | 129 ++-------- spec/controllers/vms_controller_spec.rb | 219 +++-------------- spec/end_to_end/end_to_end_spec.rb | 109 --------- spec/factories/requests.rb | 21 +- spec/factories/users.rb | 24 -- spec/factories/virtual_machine_configs.rb | 1 - spec/features/dashboard/dashboard_spec.rb | 20 +- .../requests/accept_reject_request_spec.rb | 4 +- spec/features/requests/index.html.erb_spec.rb | 4 +- .../requests/select_operating_system_spec.rb | 2 +- spec/features/sessions/user_sign_in_spec.rb | 54 +++++ spec/features/users/index.html.erb_spec.rb | 116 --------- spec/helpers/operating_systems_helper_spec.rb | 2 +- spec/helpers/requests_helper_spec.rb | 16 +- spec/helpers/vms_helper_spec.rb | 40 --- spec/lib/Node/node-example.pp | 7 - spec/lib/Node/node-example2.pp | 7 - spec/lib/Node/node-example3.pp | 7 - spec/lib/Node/node-example4.pp | 7 - spec/lib/Node/node-example5.pp | 7 - spec/lib/Node/node-example6.pp | 6 - spec/lib/Node/node-example7.pp | 7 - spec/lib/Node/node-example8.pp | 7 - spec/lib/puppetscript_spec.rb | 89 ------- spec/mailers/notification_mailer_spec.rb | 7 - .../previews/notification_mailer_preview.rb | 5 - spec/models/request_spec.rb | 229 +++--------------- spec/models/request_templates_spec.rb | 66 ----- spec/models/user_spec.rb | 17 -- spec/models/virtual_machine_config_spec.rb | 14 +- spec/spec_helper.rb | 28 +-- spec/support/git_stub.rb | 46 ---- .../application/_navbar.html.erb_spec.rb | 8 +- spec/views/hosts/show.html.erb_spec.rb | 38 ++- spec/views/projects/show.html.erb_spec.rb | 1 - spec/views/requests/edit.html.erb_spec.rb | 26 +- spec/views/requests/index.html.erb_spec.rb | 53 +++- spec/views/requests/new.html.erb_spec.rb | 68 +++--- spec/views/requests/show.html.erb_spec.rb | 17 +- spec/views/servers/edit.html.erb_spec.rb | 2 +- spec/views/servers/new.html.erb_spec.rb | 2 +- spec/views/users/show.html.erb_spec.rb | 6 +- spec/views/vms/edit.html.erb_spec.rb | 26 -- spec/views/vms/edit_config.html.erb_spec.rb | 13 +- spec/views/vms/index.html.erb_spec.rb | 51 ++-- spec/views/vms/show.html.erb_spec.rb | 53 ++-- 124 files changed, 952 insertions(+), 3058 deletions(-) delete mode 100644 app/assets/javascripts/prevent_anchor_reload.js delete mode 100644 app/mailers/notification_mailer.rb delete mode 100644 app/views/notification_mailer/notify_email.html.erb delete mode 100644 app/views/notification_mailer/notify_email.text.erb delete mode 100644 config/environment_variables.example.yml delete mode 100644 config/initializers/environment_variables.rb delete mode 100644 db/migrate/20190121160434_create_join_table_requests_responsible_users.rb delete mode 100644 db/migrate/20190129184200_add_email_notifications_to_user.rb delete mode 100644 db/migrate/20190131083251_add_description_to_virtual_machine_configs.rb delete mode 100644 db/migrate/20190131115425_rename_request_mb_to_gb.rb delete mode 100644 db/migrate/20190201091109_create_join_table_virtual_machine_config_users.rb delete mode 100644 db/migrate/20190204164433_add_projects_to_requests.rb delete mode 100644 db/migrate/20190205115620_add_port_forwarding_to_requests.rb delete mode 100644 lib/git_helper.rb delete mode 100644 lib/puppetscript.rb delete mode 100644 spec/end_to_end/end_to_end_spec.rb delete mode 100644 spec/helpers/vms_helper_spec.rb delete mode 100644 spec/lib/Node/node-example.pp delete mode 100644 spec/lib/Node/node-example2.pp delete mode 100644 spec/lib/Node/node-example3.pp delete mode 100644 spec/lib/Node/node-example4.pp delete mode 100644 spec/lib/Node/node-example5.pp delete mode 100644 spec/lib/Node/node-example6.pp delete mode 100644 spec/lib/Node/node-example7.pp delete mode 100644 spec/lib/Node/node-example8.pp delete mode 100644 spec/lib/puppetscript_spec.rb delete mode 100644 spec/mailers/notification_mailer_spec.rb delete mode 100644 spec/mailers/previews/notification_mailer_preview.rb delete mode 100644 spec/models/request_templates_spec.rb delete mode 100644 spec/support/git_stub.rb delete mode 100644 spec/views/vms/edit.html.erb_spec.rb diff --git a/.gitignore b/.gitignore index da7af7bd..b89cbb4a 100644 --- a/.gitignore +++ b/.gitignore @@ -45,10 +45,4 @@ coverage # Ignore locks .lock* -config/credentials.yml.enc - -# Ignore environment variables -/config/environment_variables.yml - -# Ignore Puppetscript folder -/private \ No newline at end of file +config/credentials.yml.enc \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 29b4c7b8..74f4b308 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,3 @@ script: after_success: # https://docs.travis-ci.com/user/environment-variables/#default-environment-variables - curl --data "commit=$TRAVIS_COMMIT" --data "branch=$TRAVIS_BRANCH" --data "eventtype=$TRAVIS_EVENT_TYPE" http://hart-deploy.epic-hpi.de/ - -env: - - GIT_REPOSITORY_URL=test_repository_url GIT_REPOSITORY_NAME=test_repository_name GITHUB_USER_NAME=test_user_name GITHUB_USER_EMAIL=test_user_email \ No newline at end of file diff --git a/Gemfile b/Gemfile index 88868b7c..7c731466 100644 --- a/Gemfile +++ b/Gemfile @@ -62,9 +62,6 @@ gem 'omniauth_openid_connect' # Allow locks on database gem 'with_advisory_lock' -# Ruby Git -gem 'git' - # Mina for deployment # Have a look in the tutorial: # https://github.com/lnikell/wiki/wiki/Deploy-rails-application-with-Mina,-Nginx-and-Puma diff --git a/Gemfile.lock b/Gemfile.lock index 0799c890..a86f1cd1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,7 +10,7 @@ GIT GIT remote: https://github.com/stympy/faker.git - revision: fcbfaa282dc6c2d4c1393bcb65d9fb647c8a5bc0 + revision: ed7c1dc6c80208bcc9a10d238a2b7a312d887a9e branch: master specs: faker (1.9.1) @@ -27,48 +27,48 @@ GIT GEM remote: https://rubygems.org/ specs: - actioncable (5.2.2) - actionpack (= 5.2.2) + actioncable (5.2.1) + actionpack (= 5.2.1) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailer (5.2.2) - actionpack (= 5.2.2) - actionview (= 5.2.2) - activejob (= 5.2.2) + actionmailer (5.2.1) + actionpack (= 5.2.1) + actionview (= 5.2.1) + activejob (= 5.2.1) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.2.2) - actionview (= 5.2.2) - activesupport (= 5.2.2) + actionpack (5.2.1) + actionview (= 5.2.1) + activesupport (= 5.2.1) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.2.2) - activesupport (= 5.2.2) + actionview (5.2.1) + activesupport (= 5.2.1) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (5.2.2) - activesupport (= 5.2.2) + activejob (5.2.1) + activesupport (= 5.2.1) globalid (>= 0.3.6) - activemodel (5.2.2) - activesupport (= 5.2.2) - activerecord (5.2.2) - activemodel (= 5.2.2) - activesupport (= 5.2.2) + activemodel (5.2.1) + activesupport (= 5.2.1) + activerecord (5.2.1) + activemodel (= 5.2.1) + activesupport (= 5.2.1) arel (>= 9.0) - activestorage (5.2.2) - actionpack (= 5.2.2) - activerecord (= 5.2.2) + activestorage (5.2.1) + actionpack (= 5.2.1) + activerecord (= 5.2.1) marcel (~> 0.3.1) - activesupport (5.2.2) + activesupport (5.2.1) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - addressable (2.6.0) + addressable (2.5.2) public_suffix (>= 2.0.2, < 4.0) aes_key_wrap (1.0.1) airbrake (5.8.1) @@ -79,7 +79,7 @@ GEM arel (9.0.0) ast (2.4.0) attr_required (1.0.1) - autoprefixer-rails (9.4.7) + autoprefixer-rails (9.3.1) execjs bcrypt (3.1.12) better_errors (2.5.0) @@ -98,7 +98,7 @@ GEM sass (>= 3.5.2) builder (3.2.3) byebug (10.0.2) - capybara (3.13.2) + capybara (3.10.1) addressable mini_mime (>= 0.1.3) nokogiri (~> 1.8) @@ -119,13 +119,13 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.1.4) - coveralls (0.8.22) - json (>= 1.8, < 3) - simplecov (~> 0.16.1) - term-ansicolor (~> 1.3) - thor (~> 0.19.4) - tins (~> 1.6) + concurrent-ruby (1.1.3) + coveralls (0.7.1) + multi_json (~> 1.3) + rest-client + simplecov (>= 0.7) + term-ansicolor + thor crass (1.0.4) debug_inspector (0.0.3) devise (4.5.0) @@ -135,23 +135,24 @@ GEM responders warden (~> 1.2.3) devise-bootstrap-views (1.1.0) - devise-i18n (1.7.1) - devise (>= 4.5) + devise-i18n (1.6.5) + devise (>= 4.4) diff-lcs (1.3) docile (1.3.1) - erubi (1.8.0) + domain_name (0.5.20180417) + unf (>= 0.0.5, < 1.0.0) + erubi (1.7.1) execjs (2.7.0) factory_bot (4.11.1) activesupport (>= 3.0.0) factory_bot_rails (4.11.1) factory_bot (~> 4.11.1) railties (>= 3.0.0) - ffi (1.10.0) + ffi (1.9.25) font-awesome-rails (4.7.0.4) railties (>= 3.2, < 6.0) formatador (0.2.5) - git (1.5.0) - globalid (0.4.2) + globalid (0.4.1) activesupport (>= 4.2.0) guard (2.15.0) formatador (>= 0.2.4) @@ -162,12 +163,14 @@ GEM pry (>= 0.9.12) shellany (~> 0.0) thor (>= 0.18.1) - hashie (3.6.0) + hashie (3.5.7) + http-cookie (1.0.3) + domain_name (~> 0.5) httpclient (2.8.3) - i18n (1.5.3) + i18n (1.1.1) concurrent-ruby (~> 1.0) io-like (0.3.0) - jaro_winkler (1.5.2) + jaro_winkler (1.5.1) jbuilder (2.8.0) activesupport (>= 4.2.0) multi_json (>= 1.2) @@ -176,8 +179,8 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (2.1.0) - json-jwt (1.10.0) - activesupport (>= 4.2) + json-jwt (1.9.4) + activesupport aes_key_wrap bindata libv8 (6.7.288.46.1) @@ -193,8 +196,11 @@ GEM mini_mime (>= 0.1.1) marcel (0.3.3) mimemagic (~> 0.3.2) - method_source (0.9.2) - mimemagic (0.3.3) + method_source (0.9.1) + mime-types (3.2.2) + mime-types-data (~> 3.2015) + mime-types-data (3.2018.0812) + mimemagic (0.3.2) mina (1.2.3) open4 (~> 1.3.4) rake @@ -203,33 +209,35 @@ GEM mina-multistage (1.0.3) mina (~> 1.0) mini_mime (1.0.1) - mini_portile2 (2.4.0) + mini_portile2 (2.3.0) mini_racer (0.2.4) libv8 (>= 6.3) minitest (5.11.3) - msgpack (1.2.6) + msgpack (1.2.4) multi_json (1.13.1) nenv (0.3.0) - net-ssh (5.1.0) + net-ssh (5.0.2) + netrc (0.11.0) nio4r (2.3.1) - nokogiri (1.10.1) - mini_portile2 (~> 2.4.0) + nokogiri (1.8.5) + mini_portile2 (~> 2.3.0) notiffany (0.1.1) nenv (~> 0.1) shellany (~> 0.0) - omniauth (1.9.0) - hashie (>= 3.4.6, < 3.7.0) + omniauth (1.8.1) + hashie (>= 3.4.6, < 3.6.0) rack (>= 1.6.2, < 3) - omniauth_openid_connect (0.2.4) + omniauth_openid_connect (0.1) addressable (~> 2.5) omniauth (~> 1.3) - openid_connect (~> 1.1) + openid_connect (~> 0.12.0) open4 (1.3.4) - openid_connect (1.1.6) + openid_connect (0.12.0) activemodel attr_required (>= 1.0.0) + json (>= 1.4.3) json-jwt (>= 1.5.0) - rack-oauth2 (>= 1.6.1) + rack-oauth2 (>= 1.3.1) swd (>= 1.0.0) tzinfo validate_email @@ -237,18 +245,18 @@ GEM webfinger (>= 1.0.1) optimist (3.0.0) orm_adapter (0.5.0) - parallel (1.13.0) - parser (2.6.0.0) + parallel (1.12.1) + parser (2.5.3.0) ast (~> 2.4.0) - pg (1.1.4) - popper_js (1.14.5) + pg (1.1.3) + popper_js (1.14.3) powerpack (0.1.2) pry (0.12.2) coderay (~> 1.1.0) method_source (~> 0.9.0) public_suffix (3.0.3) puma (3.12.0) - pundit (2.0.1) + pundit (2.0.0) activesupport (>= 3.0.0) rack (2.0.6) rack-oauth2 (1.9.3) @@ -259,48 +267,52 @@ GEM rack rack-test (1.1.0) rack (>= 1.0, < 3) - rails (5.2.2) - actioncable (= 5.2.2) - actionmailer (= 5.2.2) - actionpack (= 5.2.2) - actionview (= 5.2.2) - activejob (= 5.2.2) - activemodel (= 5.2.2) - activerecord (= 5.2.2) - activestorage (= 5.2.2) - activesupport (= 5.2.2) + rails (5.2.1) + actioncable (= 5.2.1) + actionmailer (= 5.2.1) + actionpack (= 5.2.1) + actionview (= 5.2.1) + activejob (= 5.2.1) + activemodel (= 5.2.1) + activerecord (= 5.2.1) + activestorage (= 5.2.1) + activesupport (= 5.2.1) bundler (>= 1.3.0) - railties (= 5.2.2) + railties (= 5.2.1) sprockets-rails (>= 2.0.0) - rails-controller-testing (1.0.4) - actionpack (>= 5.0.1.x) - actionview (>= 5.0.1.x) - activesupport (>= 5.0.1.x) + rails-controller-testing (1.0.2) + actionpack (~> 5.x, >= 5.0.1) + actionview (~> 5.x, >= 5.0.1) + activesupport (~> 5.x) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) - railties (5.2.2) - actionpack (= 5.2.2) - activesupport (= 5.2.2) + railties (5.2.1) + actionpack (= 5.2.1) + activesupport (= 5.2.1) method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) rainbow (3.0.0) - rake (12.3.2) + rake (12.3.1) rb-fsevent (0.10.3) - rb-inotify (0.10.0) - ffi (~> 1.0) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) rbvmomi (2.0.0) builder (~> 3.0) json (>= 1.8) nokogiri (~> 1.5) optimist (~> 3.0) - regexp_parser (1.3.0) - responders (2.4.1) - actionpack (>= 4.2.0, < 6.0) - railties (>= 4.2.0, < 6.0) + regexp_parser (1.2.0) + responders (2.4.0) + actionpack (>= 4.2.0, < 5.3) + railties (>= 4.2.0, < 5.3) + rest-client (2.0.2) + http-cookie (>= 1.0.2, < 2.0) + mime-types (>= 1.16, < 4.0) + netrc (~> 0.8) rspec-core (3.8.0) rspec-support (~> 3.8.0) rspec-expectations (3.8.2) @@ -309,7 +321,7 @@ GEM rspec-mocks (3.8.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) - rspec-rails (3.8.2) + rspec-rails (3.8.1) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) @@ -318,7 +330,7 @@ GEM rspec-mocks (~> 3.8.0) rspec-support (~> 3.8.0) rspec-support (3.8.0) - rubocop (0.63.1) + rubocop (0.60.0) jaro_winkler (~> 1.5.1) parallel (~> 1.10) parser (>= 2.5, != 2.5.1.1) @@ -326,12 +338,12 @@ GEM rainbow (>= 2.2.2, < 4.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.4.0) - rubocop-rspec (1.32.0) + rubocop-rspec (1.30.1) rubocop (>= 0.60.0) ruby-progressbar (1.10.0) ruby_dep (1.5.0) rubyzip (1.2.2) - sass (3.7.3) + sass (3.7.2) sass-listen (~> 4.0.0) sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) @@ -373,28 +385,31 @@ GEM activesupport (>= 3) attr_required (>= 0.0.5) httpclient (>= 2.4) - term-ansicolor (1.7.1) + term-ansicolor (1.7.0) tins (~> 1.0) - thor (0.19.4) + thor (0.20.0) thread_safe (0.3.6) - tilt (2.0.9) - tins (1.20.2) + tilt (2.0.8) + tins (1.19.0) turbolinks (5.2.0) turbolinks-source (~> 5.2) turbolinks-source (5.2.0) tzinfo (1.2.5) thread_safe (~> 0.1) - uglifier (4.1.20) + uglifier (4.1.19) execjs (>= 0.3.0, < 3) - unicode-display_width (1.4.1) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.5) + unicode-display_width (1.4.0) validate_email (0.1.6) activemodel (>= 3.0) mail (>= 2.2.5) validate_url (1.0.2) activemodel (>= 3.0.0) addressable - warden (1.2.8) - rack (>= 2.0.6) + warden (1.2.7) + rack (>= 1.0) web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) @@ -432,7 +447,6 @@ DEPENDENCIES factory_bot_rails faker! font-awesome-rails - git guard jbuilder (~> 2.5) jquery-rails diff --git a/app/api/v_sphere/folder.rb b/app/api/v_sphere/folder.rb index e8cfc851..e852a9f9 100644 --- a/app/api/v_sphere/folder.rb +++ b/app/api/v_sphere/folder.rb @@ -48,16 +48,8 @@ def name # Ensure that a subfolder exists and return it # folder_name is a string with the name of the subfolder def ensure_subfolder(folder_name) - subfolder = subfolders.find { |each| each.name == folder_name } - subfolder || VSphere::Folder.new(@folder.CreateFolder(name: folder_name)) - end - - # Ensure that the path relative to this folder is a valid folder and return it - # The path is an array of strings - def ensure_subfolder_by_path(path) - return self if path.empty? - - ensure_subfolder(path.first).ensure_subfolder_by_path(path[1..-1]) + folder = subfolders.find { |each| each.name == folder_name } + folder || VSphere::Folder.new(@folder.CreateFolder(name: folder_name)) end def move_here(folder_entry) diff --git a/app/api/v_sphere/host.rb b/app/api/v_sphere/host.rb index 53d5a7cb..71b7b85a 100644 --- a/app/api/v_sphere/host.rb +++ b/app/api/v_sphere/host.rb @@ -11,13 +11,6 @@ def self.all VSphere::Cluster.all.map(&:hosts).flatten end - def self.get_host(name) - VSphere::Host.all.each do |host| - return host if host.name == name - end - nil - end - def initialize(rbvmomi_host) @host = rbvmomi_host end @@ -58,18 +51,6 @@ def ==(other) equal? other end - def get_num_cpu - @host.summary.hardware.numCpuCores - end - - def get_ram_gb - (@host.summary.hardware.memorySize.to_f / 1024**2).round(2) - end - - def get_storage_gb - (@host.summary.host.datastore.sum { |datastore| datastore.summary.freeSpace }.to_f / 1024**2).round(2) - end - private def managed_folder_entry diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index 8e98d0d1..afc1d949 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -57,32 +57,9 @@ def self.find_by_name(name) root_folder.find_vm(name) end - def self.prepare_vm_names - files = Dir.entries(File.join(Puppetscript.puppet_script_path, 'Node')) - files.map! { |file| file[(5..file.length - 4)] } - files.reject!(&:nil?) - files - end - - def self.includes_user?(vm_name, user) - users = Puppetscript.read_node_file(vm_name) - users = users['admins'] + users['users'] | [] - users.include? user - end - def self.user_vms(user) - vms = [] - begin - GitHelper.open_repository Puppetscript.puppet_script_path do - vm_names = prepare_vm_names - vm_names.each do |vm_name| - vms.append(find_by_name(vm_name)) if includes_user?(vm_name, user) - end - end - rescue Git::GitExecuteError => e - Rails.logger.error(e) - end - vms + requests = user.requests.accepted + requests.map { |request| find_by_name(request.name) }.compact end def initialize(rbvmomi_vm) @@ -95,8 +72,7 @@ def name # Guest OS communication def vm_ware_tools? - tool_status = @vm&.guest&.toolsStatus - tool_status && %w[toolsOk toolsOld].include?(tool_status) + @vm.guest.toolsStatus != 'toolsNotInstalled' end def suspend_vm @@ -119,11 +95,11 @@ def reboot_guest_os # We do not provide a power_state? method which just returns a boolean, because vSphere can internally handle # more than just two power states and we might later need to respond to more states than just two def powered_on? - @vm.summary.runtime.powerState == 'poweredOn' + @vm.runtime.powerState == 'poweredOn' end def powered_off? - @vm.summary.runtime.powerState == 'poweredOff' + @vm.runtime.powerState == 'poweredOff' end # Power state @@ -158,7 +134,6 @@ def archived? def set_pending_archivation move_into pending_archivation_folder ArchivationRequest.new(name: name).save - move_into_correct_subfolder end def archivable? @@ -179,7 +154,6 @@ def set_archived move_into archived_folder archivation_request&.delete - move_into_correct_subfolder end # Reviving @@ -189,13 +163,11 @@ def pending_reviving? def set_pending_reviving move_into pending_revivings_folder - move_into_correct_subfolder end def set_revived move_into root_folder archivation_request&.delete - move_into_correct_subfolder end # Config methods @@ -205,7 +177,12 @@ def config end def ensure_config - @config ||= config || VirtualMachineConfig.create!(name: name) + unless config + @config = VirtualMachineConfig.new + @config.name = name + @config.save + end + @config end def ip @@ -216,22 +193,11 @@ def dns config&.dns || '' end - # Folder Utilities + # Utilities def move_into(folder) folder.move_here self end - def move_into_correct_subfolder - target = target_subfolder - move_into target unless target.vms(recursive: false).include? self - end - - # Users - def responsible_users - config&.responsible_users || [] - end - - # Information about the vm def boot_time @vm.runtime.bootTime end @@ -240,22 +206,14 @@ def summary @vm.summary end - def macs - @vm.macs - end - - def disks - @vm.disks - end - - def guest - @vm.guest - end - def guest_heartbeat_status @vm.guestHeartbeatStatus end + def vmwaretools_installed? + @vm.guest.toolsStatus != 'toolsNotInstalled' + end + def host_name @vm.summary.runtime.host.name end @@ -265,9 +223,13 @@ def active? end def status - return :archived if archived? - return :pending_archivation if pending_archivation? - return :pending_reviving if pending_reviving? + if archived? + return :archived + elsif pending_archivation? + return :pending_archivation + elsif pending_reviving? + return :pending_reviving + end if powered_on? :online @@ -276,91 +238,20 @@ def status end end - # this method should return all users, including the sudo users def users - users = [] - begin - GitHelper.open_repository Puppetscript.puppet_script_path do - remote_users = Puppetscript.read_node_file(name) - users = remote_users['users'] - end - rescue Git::GitExecuteError => e - Rails.logger.error(e) - end - users - end - - def commit_message(git_writer) - if git_writer.added? - 'Add ' + name - elsif git_writer.updated? - 'Update ' + name + if request + request.users else - '' - end - end - - def name_path - File.join('Name', name + '.pp') - end - - def node_path - File.join('Node', 'node-' + name + '.pp') - end - - def user_name_and_node_script(ids) - all_users = Puppetscript.read_node_file(name) - sudo_users = all_users['admins'] - new_users = User.where(id: ids) - name_script = Puppetscript.name_script(name) - node_script = Puppetscript.node_script(name, sudo_users, new_users) - [name_script, node_script] - end - - def users=(ids) - GitHelper.open_repository(Puppetscript.puppet_script_path) do |git_writer| - name_script, node_script = user_name_and_node_script(ids) - git_writer.write_file(name_path, name_script) - git_writer.write_file(node_path, node_script) - message = commit_message(git_writer) - git_writer.save(message) + [] end - rescue Git::GitExecuteError => e - Rails.logger.error(e) end def sudo_users - admins = [] - begin - GitHelper.open_repository Puppetscript.puppet_script_path do - users = Puppetscript.read_node_file(name) - admins = users['admins'] - end - rescue Git::GitExecuteError => e - Rails.logger.error(e) - end - admins - end - - def sudo_name_and_node_script(ids) - all_users = Puppetscript.read_node_file(name) - users = all_users['users'] - new_sudo_users = User.where(id: ids) - name_script = Puppetscript.name_script(name) - node_script = Puppetscript.node_script(name, new_sudo_users, users) - [name_script, node_script] - end - - def sudo_users=(ids) - GitHelper.open_repository(Puppetscript.puppet_script_path) do |git_writer| - name_script, node_script = sudo_name_and_node_script(ids) - git_writer.write_file(name_path, name_script) - git_writer.write_file(node_path, node_script) - message = commit_message(git_writer) - git_writer.save(message) + if request + request.sudo_users + else + [] end - rescue Git::GitExecuteError => e - logger.error(e) end def belongs_to(user) @@ -379,22 +270,14 @@ def ==(other) equal? other end - def request - @request ||= Request.accepted.find_by name: name + def macs + @vm.macs end private - def target_subfolder - path = [] << case status - when :archived then archived_folder.name - when :pending_reviving then pending_revivings_folder.name - when :pending_archivation then pending_archivation_folder.name - else - 'Active VMs' - end - path << responsible_users.first.human_readable_identifier if responsible_users.first - VSphere::Connection.instance.root_folder.ensure_subfolder_by_path path + def request + Request.accepted.find { |each| name == each.name } end def archivation_request diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 5b58cd0c..102f077e 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -10,18 +10,17 @@ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // -//= require jquery3 //= require rails-ujs //= require activestorage //= require turbolinks +//= require jquery3 //= require popper //= require bootstrap //= require doughnut_chart //= require filter_table -//= require prevent_anchor_reload //= require_tree . //= require select2 document.addEventListener("turbolinks:load", function() { $('.selecttwo').select2(); -}); +}); \ No newline at end of file diff --git a/app/assets/javascripts/prevent_anchor_reload.js b/app/assets/javascripts/prevent_anchor_reload.js deleted file mode 100644 index 780f1f7a..00000000 --- a/app/assets/javascripts/prevent_anchor_reload.js +++ /dev/null @@ -1,50 +0,0 @@ -document.addEventListener("turbolinks:before-visit", function (event) { - var origin = window.location.href; - var destination = event.data.url; - if (origin.match(/#/) === null) { - if (origin[origin.length - 1] === "/") { - origin = origin.substring(0, origin.length - 1); - } - } - else { - var hashIndex = origin.indexOf('#'); - if (hashIndex > 0 && origin[hashIndex - 1] === "/") - origin = "" + origin.substring(0, (hashIndex - 1)) + origin.substring(hashIndex); - } - if (destination.match(/#/) === null) { - if (destination[destination.length - 1] === "/") { - destination = destination.substring(0, destination.length - 1); - } - } - else { - var hashIndex = destination.indexOf('#'); - if (hashIndex > 0 && destination[hashIndex - 1] === "/") - destination = "" + destination.substring(0, (hashIndex - 1)) + destination.substring(hashIndex); - } - - if (origin === destination) - return; - - var shorterLength = Math.min(origin.length, destination.length); - if (((origin.match(/#/) !== null) && (destination.match(/#/) !== null)) && - (origin.indexOf("#") === destination.indexOf("#"))) { - shorterLength = Math.min(origin.indexOf("#"), destination.indexOf("#")); - } - if (origin.substring(0, shorterLength) !== destination.substring(0, shorterLength)) { - return; - } - if (destination.length > shorterLength && destination[shorterLength] === "#") { - event.preventDefault(); - var urlHashMinusHash = destination.substring(destination.indexOf('#')).substring(1); - var elem = document.getElementById(urlHashMinusHash); - if (elem != null) - elem.scrollIntoView(); - return; - } - - if (origin.length > shorterLength && origin[shorterLength] === "#") { - event.preventDefault(); - $("html, body").animate({ scrollTop: 0 }, "slow"); - return; - } -}); diff --git a/app/assets/javascripts/request.js b/app/assets/javascripts/request.js index b7c9918f..b707050a 100644 --- a/app/assets/javascripts/request.js +++ b/app/assets/javascripts/request.js @@ -1,4 +1,4 @@ -$('#request_template_id').change(function (){ +$('input:radio').click(function (){ cpus = document.getElementById('cpu'); ram = document.getElementById('ram'); storage = document.getElementById('storage'); @@ -24,16 +24,4 @@ $('#request_template_id').change(function (){ storage.value = storage_mb; os.value = operating_system_clean; } - }); - - if($('#request_port_forwarding_checkbox').prop("data-port_forwarding") == "true" || - $('#request_port_forwarding_checkbox').prop("checked") || - $('#port_field').val() || - $('#application_name_field').val()) { - $('#request_port_forwarding_checkbox').prop('checked', true); - $('#request_port_forwarding_info').addClass('show') - } - else{ - $('#request_port_forwarding_checkbox').prop('checked', false); - $('#request_port_forwarding_info').addClass('remove') - } +}); \ No newline at end of file diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 041f67e5..d7e418a6 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -87,12 +87,12 @@ body { } } -.table-flex-width { - width: auto; +.form-group { + width: 500px; } -.unread { - background-color: #e6f3ff; +.table-flex-width { + width: auto; } #btns_users_role .btn-primary.active { diff --git a/app/assets/stylesheets/requests.scss b/app/assets/stylesheets/requests.scss index bd36f34a..724e2e7d 100644 --- a/app/assets/stylesheets/requests.scss +++ b/app/assets/stylesheets/requests.scss @@ -39,14 +39,3 @@ h1{ background-color: #0C415F !important; color: #FFFFFF !important; } - -.select2-container { - width: 100% !important; -} - -.styled-form { - h5 { - font-weight: bold; - font-size: 1.2em; - } -} \ No newline at end of file diff --git a/app/assets/stylesheets/vms.scss b/app/assets/stylesheets/vms.scss index 9089fdb8..189b6598 100644 --- a/app/assets/stylesheets/vms.scss +++ b/app/assets/stylesheets/vms.scss @@ -78,10 +78,6 @@ h2{ content: "\25A0 "; } -.script-doc::before { - content: "\1F4CE"; -} - .btn-manage { font-size: 1.25rem; margin-left: 0.5rem; @@ -93,7 +89,6 @@ h2{ .btn-manage:hover { text-decoration: none; color: black; - filter: drop-shadow(2px 2px 1px $secondary); } .filter-box { diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1a1521a8..1e0176d8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -19,11 +19,11 @@ def after_sign_out_path_for(_resource) rescue_from ActiveRecord::RecordNotFound, with: :render_not_found def render_timeout - render(template: 'errors/timeout', status: 408) && nil + render(template: 'errors/timeout', status: 408) && return end def render_not_found - render(template: 'errors/not_found', status: 404) && nil + render(template: 'errors/not_found', status: 404) && return end def authenticate_employee diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 04a92a7c..42a6ed06 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -8,7 +8,7 @@ class DashboardController < ApplicationController def index redirect_to '/users/sign_in' if current_user.nil? - @vms = current_user.nil? ? VSphere::VirtualMachine.all : VSphere::VirtualMachine.user_vms(current_user) + @vms = VSphere::VirtualMachine.all @hosts = VSphere::Host.all @notifications = Notification.where(user: current_user).slice(0, number_of_notifications) end diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index 656be61f..d9bd2a63 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -3,7 +3,6 @@ require 'vmapi.rb' class HostsController < ApplicationController attr_reader :hosts - before_action :authenticate_admin def index @hosts = VSphere::Host.all @@ -12,7 +11,7 @@ def index def new; end def show - @host = VSphere::Host.get_host(params[:id]) + @host = VmApi.instance.get_host(params[:id]) render(template: 'errors/not_found', status: :not_found) if @host.nil? end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 975428d6..a1830a56 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -25,11 +25,6 @@ def create @project = Project.new(project_params) if @project.save redirect_to action: :index - @project.responsible_users.each do |each| - each.notify('Project created', - "The project #{@project.name} with you as the responsible has been created: " + - url_for(controller: :projects, action: 'show', id: @project.id)) - end else @selected_user_ids = project_params[:responsible_user_ids] render :new diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb index 32ddf7cf..aaa84095 100644 --- a/app/controllers/requests_controller.rb +++ b/app/controllers/requests_controller.rb @@ -3,15 +3,14 @@ class RequestsController < ApplicationController include OperatingSystemsHelper include RequestsHelper - before_action :set_request, only: %i[show edit update push_to_git destroy request_state_change] + before_action :set_request, only: %i[show edit update destroy request_state_change] before_action :authenticate_employee before_action :authenticate_state_change, only: %i[request_change_state] # GET /requests # GET /requests.json def index - requests = current_user.admin? ? Request.all : Request.select { |r| r.user == current_user } - split_requests(requests) + @requests = current_user.admin? ? Request.all : Request.select { |r| r.user == current_user } end # GET /requests/1 @@ -30,7 +29,6 @@ def new # GET /requests/1/edit def edit redirect_to @request unless @request.pending? - @request_templates = RequestTemplate.all end def notify_users(title, message) @@ -43,11 +41,11 @@ def notify_users(title, message) # POST /requests.json def create prepare_params - @request = Request.new(request_params.merge(user: current_user)) - @request.assign_sudo_users(request_params[:sudo_user_ids]) + respond_to do |format| - if enough_resources && @request.save + if @request.save + @request.assign_sudo_users(request_params[:sudo_user_ids][1..-1]) successful_save(format) else unsuccessful_action(format, :new) @@ -60,11 +58,17 @@ def create def update respond_to do |format| prepare_params - @request.assign_sudo_users(request_params[:sudo_user_ids]) - @request.accept! if @request.update(request_params) + @request.accept! + @request.save notify_request_update - safe_create_vm_for format, @request + vm = @request.create_vm + if vm + format.html { redirect_to({ controller: :vms, action: 'edit_config', id: vm.name }, method: :get, notice: I18n.t('request.successfully_updated_and_vm_created')) } + format.json { render status: :ok } + else + format.html { redirect_to requests_path, alert: 'VM could not be created, please create it manually in vSphere!' } + end else unsuccessful_action format, :edit end @@ -74,8 +78,9 @@ def update def reject @request = Request.find params[:id] respond_to do |format| - @request.reject! if @request&.update(rejection_params) + @request.reject! + @request.save notify_request_update format.html { redirect_to requests_path, notice: "Request '#{@request.name}' rejected!" } format.json { render status: :ok, action: :index } @@ -95,27 +100,8 @@ def destroy end end - # Creates puppet files for request and pushes the created files into a git repository - def push_to_git - response = @request.push_to_git - redirect_to requests_path, response - end - private - def safe_create_vm_for(format, request) - vm, warning = request.create_vm - if vm - notices = warning.nil? ? { notice: 'VM was successfully created!' } : { alert: warning } - format.html { redirect_to({ controller: :vms, action: 'edit_config', id: vm.name }, { method: :get }.merge(notices)) } - format.json { render status: :ok } - else - format.html { redirect_to requests_path, alert: 'VM could not be created, there are no hosts available in vSphere!' } - end - rescue RbVmomi::Fault => fault - format.html { redirect_to requests_path, alert: "VM could not be created, error: \"#{fault.message}\"" } - end - def host_url request.base_url end @@ -135,12 +121,7 @@ def notify_request_update return if @request.pending? if @request.accepted? - ([@request.user] + User.admin).each do |each| - each.notify('Request has been accepted', @request.description_text(host_url)) - end - @request.users.each do |each| - each.notify('You have (sudo) rights on a new VM', @request.description_text(host_url)) - end + notify_users('Request has been accepted', @request.description_text(host_url)) elsif @request.rejected? message = @request.description_text host_url message += @request.rejection_information.empty? ? '' : "\nwith comment: #{@request.rejection_information}" @@ -161,78 +142,20 @@ def unsuccessful_action(format, method) end # Storage and RAM are displayed in GB but internally stored in MB. - # sudo_user_ids always contain one empty element which must be removed def prepare_params - request_parameters = params[:request] - return unless request_parameters - - request_parameters[:name] &&= replace_whitespaces(request_parameters[:name]) - request_parameters[:sudo_user_ids] &&= request_parameters[:sudo_user_ids][1..-1] - - # the user_ids must contain the ids of ALL users, sudo or not - request_parameters[:user_ids] ||= [] - request_parameters[:user_ids] += request_parameters[:sudo_user_ids] if request_parameters[:sudo_user_ids] + params[:request][:name] = replace_whitespaces(params[:request][:name]) if params[:request][:name] + params[:request][:ram_mb] = gb_to_mb(params[:request][:ram_mb].to_i) if params[:request][:ram_mb] + params[:request][:storage_mb] = gb_to_mb(params[:request][:storage_mb].to_i) if params[:request][:storage_mb] end # Never trust parameters from the scary internet, only allow the white list through. def request_params - params.require(:request).permit(:name, :cpu_cores, :ram_gb, :storage_gb, :operating_system, - :port, :application_name, :description, :comment, :project_id, :port_forwarding, - :rejection_information, responsible_user_ids: [], user_ids: [], sudo_user_ids: []) + params.require(:request).permit(:name, :cpu_cores, :ram_mb, :storage_mb, :operating_system, + :port, :application_name, :description, :comment, + :rejection_information, user_ids: [], sudo_user_ids: []) end def rejection_params params.require(:request).permit(:rejection_information) end - - def puppet_node_script(request) - request.generate_puppet_node_script - end - helper_method :puppet_node_script - - def puppet_name_script(request) - request.generate_puppet_name_script - end - - helper_method :puppet_name_script - - def split_requests(requests) - @pending_requests = requests.select(&:pending?) - @resolved_requests = requests.reject(&:pending?) - end - - def enough_resources - hosts = VSphere::Host.all - - # get max host resources - max_cpu_host = hosts[0] - max_ram_host = hosts[0] - max_storage_host = hosts[0] - - hosts.each do |host| - # check if the host could handle the vm - host_num_cpu = host.get_num_cpu - host_ram = host.get_ram_gb - host_free_hdd = host.get_storage_gb - - if (request_params[:cpu_cores].to_i <= host_num_cpu) && (request_params[:ram_mb].to_i <= host_ram) && (request_params[:storage_mb].to_i <= host_free_hdd) - return true - end - - # get hosts with max resources per category - max_cpu_host = host if host_num_cpu > max_cpu_host.get_num_cpu - - max_ram_host = host if host_ram > max_ram_host.get_ram_gb - - max_storage_host = host if host_free_hdd > max_storage_host.get_storage_gb - end - - max_cpu_host_msg = "cores: #{max_cpu_host.get_num_cpu}, ram: #{max_cpu_host.get_ram_gb / 1024}GB, hdd: #{max_cpu_host.get_storage_gb / 1024}GB" - max_ram_host_msg = "cores: #{max_ram_host.get_num_cpu}, ram: #{max_ram_host.get_ram_gb / 1024}GB, hdd: #{max_ram_host.get_storage_gb / 1024}GB" - max_storage_host_msg = "cores: #{max_storage_host.get_num_cpu}, ram: #{max_storage_host.get_ram_gb / 1024}GB, hdd: #{max_storage_host.get_storage_gb / 1024}GB" - - error_message = "Requested VM resources are too high! Most Powerful Hosts are: Max Core Host(#{max_cpu_host_msg}) Max RAM Host(#{max_ram_host_msg}) Max HDD Host(#{max_storage_host_msg}) " - @request.errors[:base] << error_message - false - end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c7819232..5eeb6c77 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,7 +8,7 @@ class UsersController < ApplicationController before_action :authenticate_admin, only: %i[index update_role] def index - @users = User.search(params[:search], params[:role]) + @users = User.all end def show; end @@ -25,19 +25,14 @@ def update def update_role @user = User.find(params[:id]) - former_role = @user.role @user.update(role: params[:role]) - time = Time.zone.now.strftime('%d/%m/%Y') - @user.notify('Changed role', - "Your role has changed from #{former_role} to #{@user.role} on #{time}: " + - url_for(controller: :users, action: 'show', id: @user.id)) redirect_to users_path end private def user_params - params.require(:user).permit(:ssh_key, :email_notifications) + params.require(:user).permit(:ssh_key) end def authenticate_current_user diff --git a/app/controllers/vms_controller.rb b/app/controllers/vms_controller.rb index 2692e82d..3ba9c524 100644 --- a/app/controllers/vms_controller.rb +++ b/app/controllers/vms_controller.rb @@ -1,23 +1,24 @@ # frozen_string_literal: true require 'vmapi.rb' -require './app/api/v_sphere/virtual_machine' - class VmsController < ApplicationController - rescue_from RbVmomi::Fault, with: :not_enough_resources - attr_reader :vms include VmsHelper before_action :authenticate_admin, only: %i[archive_vm edit_config update_config] - before_action :authorize_vm_access, only: %i[show edit update] - before_action :authenticate_root_user_or_admin, only: %i[change_power_state suspend_vm shutdown_guest_os reboot_guest_os reset_vm] + before_action :authorize_vm_access, only: %i[show] + before_action :authenticate_root_user, only: %i[change_power_state suspend_vm shutdown_guest_os reboot_guest_os reset_vm] def index initialize_vm_categories filter_vm_categories current_user unless current_user.admin? end + def destroy + # params[:id] is actually the name of the vm, since the vsphere backend doesn't identify vms by IDs + # VSphere::VirtualMachine.delete_vm(params[:id]) + end + def create VmApi.instance.create_vm(params[:cpu], params[:ram], params[:capacity], params[:name]) redirect_to action: :index @@ -41,47 +42,26 @@ def update_config redirect_to requests_path, notice: 'Could not update the configuration' end else - flash[:alert] = 'Configuration could not be found!' - redirect_to controller: :vms, action: 'index' - end - end - - def edit - return render(template: 'errors/not_found', status: :not_found) if @vm.nil? - - @sudo_user_ids = @vm.sudo_users.map(&:id) - @non_sudo_user_ids = @vm.users.map(&:id) - @description = @vm.ensure_config.description - end - - def update - notify_changed_users(@vm.sudo_users.map(&:id), params[:vm_info][:sudo_user_ids].map(&:to_i), true, @vm.name) - notify_changed_users(@vm.users.map(&:id), params[:vm_info][:non_sudo_user_ids].map(&:to_i), false, @vm.name) - @vm.sudo_users = params[:vm_info][:sudo_user_ids] - @vm.users = params[:vm_info][:non_sudo_user_ids] - @vm.config.description = params[:vm_info][:description] - unless @vm.config.save - flash[:error] = 'Description couldn\'t be saved.' - redirect_to edit_vm_path(@vm.name) + redirect_to controller: :vms, action: 'index', notice: 'Configuration could not be found!' end - - redirect_to vm_path(@vm.name) end def request_vm_archivation @vm = VSphere::VirtualMachine.find_by_name params[:id] return if !@vm || @vm.archived? || @vm.pending_archivation? + User.admin.each do |each| + each.notify("VM #{@vm.name} has been requested to be archived", + "The VM has been shut down and has to be archived.\n#{url_for(controller: :vms, action: 'show', id: @vm.name)}") + end @vm.users.each do |each| each.notify("Your VM #{@vm.name} has been requested to be archived", - "The VM will soon be archived and for that it will then be shut down.\nIf you still need this VM you can stop the archiving of this VM within three days.\n" + + "The VM has been shut down and will soon be archived.\nYou can raise an objection to this on the vms overview site\n" + url_for(controller: :vms, action: 'show', id: @vm.name)) end @vm.set_pending_archivation redirect_to controller: :vms, action: 'show', id: @vm.name - @admin_ids = User.admin.pluck(:id) - ApplicationJob.set(wait: 3.days).perform_later(@admin_ids, @vm.name, url_for(controller: :vms, action: 'show', id: @vm.name)) end def request_vm_revive @@ -172,11 +152,10 @@ def initialize_vm_categories end def filter_vm_categories(user) - user_vms = VSphere::VirtualMachine.user_vms(user) - @vms = @vms.select { |each| user_vms.include?(each) } - @archived_vms = @archived_vms.select { |each| user_vms.include?(each) } - @pending_archivation_vms = @pending_archivation_vms.select { |each| user_vms.include?(each) } - @pending_reviving_vms = @pending_reviving_vms.select { |each| user_vms.include?(each) } + @vms = @vms.select { |each| each.belongs_to user } + @archived_vms = @archived_vms.select { |each| each.belongs_to user } + @pending_archivation_vms = @pending_archivation_vms.select { |each| each.belongs_to user } + @pending_reviving_vms = @pending_reviving_vms.select { |each| each.belongs_to user } end def authorize_vm_access @@ -190,15 +169,10 @@ def config_params params.require(:virtual_machine_config).permit(:ip, :dns) end - def authenticate_root_user_or_admin + def authenticate_root_user @vm = VSphere::VirtualMachine.find_by_name(params[:id]) return unless @vm redirect_to vms_path unless current_user.admin? || @vm.sudo_users.include?(current_user) end - - def not_enough_resources(exception) - redirect_back(fallback_location: root_path) - flash[:alert] = exception.message - end end diff --git a/app/helpers/operating_systems_helper.rb b/app/helpers/operating_systems_helper.rb index 469298bb..3ca4391d 100644 --- a/app/helpers/operating_systems_helper.rb +++ b/app/helpers/operating_systems_helper.rb @@ -4,6 +4,6 @@ module OperatingSystemsHelper def operating_system_options options = OperatingSystem.all.map(&:name) options.unshift 'none' - options.push 'other (write in Comment)' + options.push 'other(write in Comment)' end end diff --git a/app/helpers/requests_helper.rb b/app/helpers/requests_helper.rb index 5fee3dd3..95ce1636 100644 --- a/app/helpers/requests_helper.rb +++ b/app/helpers/requests_helper.rb @@ -1,8 +1,15 @@ # frozen_string_literal: true module RequestsHelper - # replaces all whitespaces and special characters by a hyphen and makes the string to lower case def replace_whitespaces(name) - name.parameterize + name.parameterize(preserve_case: true) + end + + def mb_to_gb(megabytes) + megabytes / 1000 + end + + def gb_to_mb(gigabytes) + gigabytes * 1000 end end diff --git a/app/helpers/vms_helper.rb b/app/helpers/vms_helper.rb index bd521530..7073f50f 100644 --- a/app/helpers/vms_helper.rb +++ b/app/helpers/vms_helper.rb @@ -32,17 +32,4 @@ def status_for(vm) 'offline' end end - - def notify_changed_users(old_list, new_list, sudo_lists, vm_name) - removed_users = old_list - new_list - removed_users.each do |user_id| - User.find(user_id).notify('Sudo rights revoked', "Your sudo rights on VM '#{vm_name}' have been revoked.") if sudo_lists - User.find(user_id).notify('User rights revoked', "Your user rights on VM '#{vm_name}' have been revoked.") unless sudo_lists - end - added_users = new_list - old_list - added_users.each do |user_id| - User.find(user_id).notify('Sudo rights granted', "You have been made sudo user on VM '#{vm_name}'") if sudo_lists - User.find(user_id).notify('User rights granted', "You have been made user on VM '#{vm_name}'") unless sudo_lists - end - end end diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index a8b464ff..d92ffddc 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -1,12 +1,4 @@ # frozen_string_literal: true class ApplicationJob < ActiveJob::Base - queue_as :send_notification - - def perform(admins_ids, vm_name, vm_url) - admins = User.where(id: admins_ids) - admins.each do |admin| - admin.notify("VM #{vm_name} has been requested to be archived", "The VM has been shut down and has to be archived.\n#{vm_url}") - end - end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 07429ea5..d84cb6e7 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true class ApplicationMailer < ActionMailer::Base - default from: 'noreply@vm-portal.com' + default from: 'from@example.com' layout 'mailer' end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb deleted file mode 100644 index d4a267d1..00000000 --- a/app/mailers/notification_mailer.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class NotificationMailer < ApplicationMailer - default from: 'notifications@vm-portal.com' - - def notify_email - @user = params[:user] - @message = params[:message] - @title = params[:title] - mail(to: @user.email, subject: @title) - end -end diff --git a/app/models/archivation_request.rb b/app/models/archivation_request.rb index c46af9d9..4dd5f8ab 100644 --- a/app/models/archivation_request.rb +++ b/app/models/archivation_request.rb @@ -5,4 +5,4 @@ def can_be_executed? three_days = 60 * 60 * 24 * 3 Time.now >= created_at + three_days end -end +end \ No newline at end of file diff --git a/app/models/request.rb b/app/models/request.rb index 96336081..381199ff 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -4,37 +4,27 @@ class Request < ApplicationRecord has_many :users_assigned_to_requests has_many :users, through: :users_assigned_to_requests belongs_to :user - belongs_to :project - has_and_belongs_to_many :responsible_users, class_name: 'User', join_table: 'requests_responsible_users' - - before_save do - users_assigned_to_requests.each(&:save) - end attr_accessor :sudo_user_ids MAX_NAME_LENGTH = 20 MAX_CPU_CORES = 64 - MAX_RAM_GB = 256 - MAX_STORAGE_GB = 1_000 + MAX_RAM_MB = 256_000 + MAX_STORAGE_MB = 1_000_000 enum status: %i[pending accepted rejected] validates :name, length: { maximum: MAX_NAME_LENGTH, message: 'only allows a maximum of %{count} characters' }, - format: { with: /\A[a-z0-9\-]+\z/, message: 'only letters and numbers allowed' }, + format: { with: /\A[a-zA-Z1-9\-\s]+\z/, message: 'only letters and numbers allowed' }, uniqueness: true - validates :responsible_users, :project_id, :cpu_cores, :ram_gb, :storage_gb, :operating_system, :description, presence: true + validates :cpu_cores, :ram_mb, :storage_mb, :operating_system, :description, presence: true validates :cpu_cores, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_CPU_CORES } - validates :ram_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_RAM_GB } - validates :storage_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_STORAGE_GB } - with_options if: :port_forwarding do |request| - request.validates :port, presence: true, numericality: { only_integer: true } - request.validates :application_name, presence: true - end + validates :ram_mb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_RAM_MB } + validates :storage_mb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_STORAGE_MB } def description_text(host_name) description = "- VM Name: #{name}\n" - description += "- Responsible: #{responsible_users.first.name}\n" + description += "- Responsible: TBD\n" description += comment.empty? ? '' : "- Comment: #{comment}\n" description += url(host_name) + "\n" description @@ -49,13 +39,12 @@ def reject! end def assign_sudo_users(sudo_user_ids) - assign_attributes(users_assigned_to_requests: users_assigned_to_requests - sudo_user_assignments) sudo_user_ids&.each do |id| assignment = users_assigned_to_requests.find { |an_assignment| an_assignment.user_id == id.to_i } - if assignment.nil? - users_assigned_to_requests.new(sudo: true, user_id: id) + if !assignment.nil? + assignment.update_attribute(:sudo, true) else - assignment.assign_attributes(sudo: true) + users_assigned_to_requests.create(sudo: true, user_id: id) end end end @@ -73,55 +62,9 @@ def non_sudo_user_assignments end def create_vm + folder = VSphere::Connection.instance.root_folder clusters = VSphere::Cluster.all - return nil, nil unless clusters.first - - warning = nil - begin - push_to_git - rescue Git::GitExecuteError => e - logger.error(e) - warning = "Your VM was created, but users could not be associated with the VM! Push to git failed, error: \"#{e.message}\"" - end - [create_vm_in_cluster(clusters.first), warning] - end - - def create_vm_in_cluster(cluster) - vm = VSphere::Connection.instance.root_folder.create_vm(cpu_cores, gibi_to_mibi(ram_gb), gibi_to_kibi(storage_gb), name, cluster) - vm.ensure_config.responsible_users = responsible_users - vm.config.description = description - vm.config.save - vm.move_into_correct_subfolder - vm - end - - # Error handling has been moved into create_vm to provide easier feedback for the user - def push_to_git - GitHelper.open_repository(Puppetscript.puppet_script_path) do |git_writer| - git_writer.write_file(File.join('Node', "node_#{name}.pp"), generate_puppet_node_script) - git_writer.write_file(File.join('Name', "#{name}.pp"), generate_puppet_name_script) - git_writer.save(commit_message(git_writer)) - end - end - - def commit_message(git_writer) - if git_writer.added? - 'Add ' + name - elsif git_writer.updated? - 'Update ' + name - else - '' - end - end - - def generate_puppet_node_script - admin_users = users_assigned_to_requests.select(&:sudo).to_a - admin_users.map!(&:user) - Puppetscript.node_script(name, admin_users, users.to_a) - end - - def generate_puppet_name_script - Puppetscript.name_script(name) + folder.create_vm(cpu_cores, ram_mb, storage_mb, name, clusters.first) if clusters.first end private @@ -129,12 +72,4 @@ def generate_puppet_name_script def url(host_name) Rails.application.routes.url_helpers.request_url self, host: host_name end - - def gibi_to_mibi(gibi) - gibi * 1024 - end - - def gibi_to_kibi(gibi) - gibi * 1024**2 - end end diff --git a/app/models/request_template.rb b/app/models/request_template.rb index 4dd5a500..d8296885 100644 --- a/app/models/request_template.rb +++ b/app/models/request_template.rb @@ -1,8 +1,12 @@ # frozen_string_literal: true class RequestTemplate < ApplicationRecord + MAX_CPU_CORES = 64 + MAX_RAM_GB = 256 + MAX_STORAGE_GB = 1_000 + validates :name, :cpu_cores, :ram_gb, :storage_gb, :operating_system, presence: true - validates :cpu_cores, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: Request::MAX_CPU_CORES } - validates :ram_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: Request::MAX_RAM_GB } - validates :storage_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: Request::MAX_STORAGE_GB } + validates :cpu_cores, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_CPU_CORES } + validates :ram_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_RAM_GB } + validates :storage_gb, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: MAX_STORAGE_GB } end diff --git a/app/models/user.rb b/app/models/user.rb index 085e8c45..225010cd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,10 +7,7 @@ class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable - before_create :set_user_id - - after_save :update_repository - + after_create :set_user_id after_initialize :set_default_role, if: :new_record? devise :database_authenticatable, :registerable, @@ -21,7 +18,6 @@ class User < ApplicationRecord has_many :users_assigned_to_requests has_many :requests, through: :users_assigned_to_requests has_many :servers - has_and_belongs_to_many :request_responsibilities, class_name: 'Request', join_table: 'requests_responsible_users' validates :first_name, presence: true validates :last_name, presence: true validate :valid_ssh_key @@ -41,22 +37,10 @@ def notify_slack(message) end end - def self.search(term, role) - if term - users = where('first_name LIKE ? or last_name LIKE ?', "%#{term}%", "%#{term}%") - users = users.where(role: role) if role && role != '' - users.order(last_name: :asc) - else - order(last_name: :asc) - end - end - # notifications def notify(title, message) notify_slack("*#{title}*\n#{message}") - NotificationMailer.with(user: self, title: title.to_s, message: message.to_s).notify_email.deliver_now if email_notifications - notification = Notification.new title: title, message: message notification.user_id = id notification.read = false @@ -73,34 +57,21 @@ def name "#{first_name} #{last_name}" end - def human_readable_identifier - email.split(/@/).first - end - def valid_ssh_key errors.add(:danger, 'Invalid SSH-Key') unless valid_ssh_key? end def self.from_omniauth(auth) - find_or_create_by(provider: auth.provider, uid: auth.uid) do |user| + where(provider: auth.provider, uid: auth.uid).first_or_create do |user| user.email = auth.info.email user.password = Devise.friendly_token[0, 20] user.role = :user + user.first_name = auth.info.first_name user.last_name = auth.info.last_name end end - def self.from_mail_identifier(mail_id) - all.each do |user| - return user if user.email.split('@').first.casecmp(mail_id).zero? - end - end - - def vm_infos - VmApi.instance.user_vms(self) - end - def vms VSphere::VirtualMachine.user_vms self end @@ -123,27 +94,10 @@ def set_user_id else Rails.configuration.start_user_id end + save end end - def update_repository - GitHelper.open_repository(Puppetscript.puppet_script_path) do |git_writer| - git_writer.write_file(File.join('Init', 'init.pp'), generate_puppet_init_script) - message = if git_writer.added? - 'Create init.pp' - else - "Add #{name}" - end - git_writer.save(message) - end - rescue Git::GitExecuteError => e - logger.error(e) - end - - def generate_puppet_init_script - Puppetscript.init_scrit(User.all) - end - def valid_ssh_key? !ssh_key? || SSHKey.valid_ssh_public_key?(ssh_key) end diff --git a/app/models/virtual_machine_config.rb b/app/models/virtual_machine_config.rb index f0cd2684..c10b111a 100644 --- a/app/models/virtual_machine_config.rb +++ b/app/models/virtual_machine_config.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true class VirtualMachineConfig < ApplicationRecord - has_and_belongs_to_many :responsible_users, class_name: 'User' end diff --git a/app/views/application/_navbar.html.erb b/app/views/application/_navbar.html.erb index 1ba8523c..64a72504 100644 --- a/app/views/application/_navbar.html.erb +++ b/app/views/application/_navbar.html.erb @@ -13,11 +13,9 @@ - <% if current_user.admin? %> - - <% end %> + diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index b81c1719..bb48f5fa 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -19,7 +19,7 @@ - + <% @vms.each do |vm| %> <%= vm.name %> @@ -61,12 +61,12 @@ <%= host.connection_state %>