diff --git a/CHANGELOG b/CHANGELOG index 8450cc2..f03382f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,31 @@ CHANGELOG ========= +2.0: 2014-11 +--------------- +Cloud-Control becomes Mission_Control! + +New Features: +- New install menu. +- Rundeck can now be installed by Misson_Control. +- Docker support. (docker-control project) +- A chef-server container can now be installed. Accessible via HTTPS/4443. +- All the variables can be edited in the "vars" file. + +Updates: +- The Rundeck project "cloud-control" got renamed to "kvm-control". +- Added new DB field "chef_installed". +- Ditched "file" backend. +- Floating ips for KVM guests can now be added separately. +- Every projects will be separated from each other in #{data_folder}. +- Smarter way to detect if Rundeck is running or not while installing Mission_Control. +- All gems will be install with `bundle install`. +- MySQL database name can now be changed via a variable. +- The KVM table name can now be changed via a variable. +- Installer won't download the same source image several times if previously downloaded. +- The ssh_keys variable can now be edited in the "vars" file. +- The userdata templates can now be generated with more than one ssh-key. + 1.4: 2014-10-11 --------------- Updates: @@ -45,19 +70,18 @@ New feature: - Upload "ready to use" Rundeck Jobs automatically. Updates: -- Ditched RVM, using ruby package instead. -- Rename "rundeck_scripts" directory to "lib". +- Ditched RVM, using ruby1.9 package instead. +- Renamed "rundeck_scripts" directory to "lib". - The setup script will now create /srv/cloud-control and generate/copy all the files and directories needed. -- Moved all the Ruby script to actual Rundeck Jobs. +- Moved all the Ruby scripts to actual Rundeck Jobs. - Added "data_folder" variable to the setup script. - Removed unneeded HOME variable from ENV file. - Removed unneeded curb gem from Gemfile. -- get_images.rb is now using the curb gem instead of a wget system call. - RUNDECK_SCRIPTS variable got renamed to LIB. - Generate Rundeck Jobs XMLs based on user environment. - build-essential added to packages list. -- Add rundeck user to libvirts and kvm Unix groups. -- The setup script will now fetch the current Trusty 14.04 LTS version from: https://cloud-images.ubuntu.com +- Installer will add the rundeck user to libvirtd and kvm Unix groups. +- The install script will now wget the current cloud-image of Ubuntu Trusty 14.04 LTS from: https://cloud-images.ubuntu.com - Added .gitignore file. - Added CHANGELOG file. @@ -68,7 +92,7 @@ New features: - New vnc_port field added to backend DBs. Updates: -- bzr package no needed anymore. +- bzr package not needed anymore. - cloud-utils package added. - Removed CLOUD_UTILS variable. - source.include? "origin" to "img". diff --git a/README.md b/README.md index 9f5f11f..f91e058 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,43 @@ -Description ------------ +#Mission_Control -Cloud-Control is a Rundeck/KVM project that lets you create/start/shutdown/destroy virtual machines. You can choose to start a new virtual machine with an ISO or an Ubuntu Cloud image. +##Description -Requirements -============ +Mission_Control is a set of Rundeck projects that lets you `create / start / shutdown / destroy` virtual machines and containers. -Rundeck should be installed. +##Installation -The hypervisor should have `Virtual Technology` enabled. You can test this prior the installation by running: +You will need to execute the `install` script to install all the required packages, gems and lay down the configuration files. - ubuntu@cbuisson:~$ egrep -c '(vmx|svm)' /proc/cpuinfo - #Anything but 0 is good. + ./install -And after the installation: +**Variables:** You can edit the `vars` file to reflect your current environment or what you want. You can paste your SSH public keys here. - ubuntu@cbuisson:~$ kvm-ok - INFO: /dev/kvm exists - KVM acceleration can be used +The install process will display a menu where you can choose to install any feature that you want. +##Environment -Installation -============ +Mission_Control has been developed for **Ubuntu Trusty 14.04 LTS**. -You will need to execute the `install` script to install all the required packages and gems. -Cloud-Control is using RVM. +#*kvm-control* - ./install +You can choose to start a new virtual machine with an ISO or an Ubuntu Cloud image. -Environment ------------ +When an Ubuntu Cloud image is used to launch a new instance, the vm will get a static IP. ISO's on the other hand, will get a DHCP ip. -Cloud-Control has been developed for **Ubuntu Trusty 14.04 LTS**. +###Network types: +*Netmask*: Mission_Control is setup to work on with Class C IPs, therefore the netmask is hard coded to: 255.255.255.0 -**Floating IPs**: +####Floating IPs -You will need to edit the `install` file and add: +You will need to edit the `vars` file and add: - - A backend type (*flat file, MySQL or PostgreSQL*) + - The interface out (**Must be br0** if using floating static IPs!) + - A backend type (*MySQL or PostgreSQL*) - Start IP (*i.e 192.168.0.1*) - End IP (*i.e 192.168.0.100*) - Gateway IP (*i.e 192.168.0.254*) -Cloud-Control will assign floating IPs to the KVM guests. Those floating IPs should be able to reach the hypervisor's IP and the gateway. You need to specify a floating IP range for the guests and a gateway to route out. +Mission_Control will assign floating IPs to the KVM guests. Those floating IPs should be able to reach the hypervisor's IP and the gateway. You need to specify a floating IP range for the guests and a gateway to route out. **NAT IPs**: @@ -54,19 +49,39 @@ You can choose to assign a floating IP or a NAT IP when launching a new guest in Deleting the KVM guest will release both the IP (floating or NAT) and the VNC port. -Templates ---------- +#*chef_server-control* - - **ssh-key**: You can add your public key to *templates/ssh_key*. - - **netmask**: Cloud-Control is setup to work on with Class C IPs, therefore the netmask is hard coded to: 255.255.255.0 +This is a Docker container that come with Chef Server 11 already installed. Mission_Control will download and launch this container if you want to. It will also grab the Knife admin keys and configure the Rundeck user to be able to use Knife. -When an Ubuntu Cloud image is used to launch a new instance, the vm will get a static IP. +#*docker-control* -ISO's on the other hand, will get a DHCP ip. +You can manage Docker containers and images with this project. Assumptions ----------- - - VMs will reach the internet trough the hypervisor via `br0`, edit the KVM guests' XML template to reflect your environment. +###kvm-control: + + - VMs will reach the internet trough the hypervisor via `br0` if floating IP selected. While using NAT, `virbr0` will be used. - If a guest is launched with the NAT option, `virbr0` (192.168.122.1) will be used to route out. + +###chef_server-control: + + - The Docker Chef_Server will be accessible via HTTPS:4443 + +Requirements +----------- + +###KVM + +The hypervisor should have `Virtual Technology` enabled. You can test this prior the installation by running: + + ubuntu@cbuisson:~$ egrep -c '(vmx|svm)' /proc/cpuinfo + #Anything but 0 is good. + +And after the installation: + + ubuntu@cbuisson:~$ kvm-ok + INFO: /dev/kvm exists + KVM acceleration can be used diff --git a/docker/rundeck_jobs.xml b/docker/rundeck_jobs.xml new file mode 100644 index 0000000..2240ee6 --- /dev/null +++ b/docker/rundeck_jobs.xml @@ -0,0 +1,86 @@ + + + 1804c8af-d5b1-42d8-be98-6107e8c1506d + INFO + + + docker rmi $(docker images -q) + + + Delete all Docker images. + Delete all images + + docker-control + + 1804c8af-d5b1-42d8-be98-6107e8c1506d + + + 862b23c4-b269-49d7-b47b-784102a83997 + INFO + + + docker rm -f $(docker ps -q) + + + Delete all stopped containers! + Delete all non-running containers + + docker-control + + 862b23c4-b269-49d7-b47b-784102a83997 + + + cd820882-15bf-4f30-b7b3-ec4e60dd548a + INFO + + + + + + + List them all! + List all containers + + docker-control + + cd820882-15bf-4f30-b7b3-ec4e60dd548a + + + bbf54c33-fc96-48eb-a61e-f887917e6abe + INFO + + + + + + + List only the running containers + List all running containers + + docker-control + + bbf54c33-fc96-48eb-a61e-f887917e6abe + + + 0ad015fb-9883-4f11-850e-effd25517d37 + INFO + + + + + + + List all local images + List all images + + docker-control + + 0ad015fb-9883-4f11-850e-effd25517d37 + + diff --git a/docker/template/rundeck_jobs-chef.xml.erb b/docker/template/rundeck_jobs-chef.xml.erb new file mode 100644 index 0000000..e21a38e --- /dev/null +++ b/docker/template/rundeck_jobs-chef.xml.erb @@ -0,0 +1,96 @@ + + + a37ea70a-b691-4fad-a78e-f049000850eb + INFO + + + + + + + + + + + Find <%= CHEF_SERVER_CONTAINER_NAME %> image and container ID then delete all the files. + Remove <%= CHEF_SERVER_CONTAINER_NAME %> container + + chef_server-control + + a37ea70a-b691-4fad-a78e-f049000850eb + + + a82e287f-e142-40d8-ad29-f065ac2e2893 + INFO + + + + + + + Start container: <%= CHEF_SERVER_CONTAINER_NAME %> + Start <%= CHEF_SERVER_CONTAINER_NAME %> + + chef_server-control + + a82e287f-e142-40d8-ad29-f065ac2e2893 + + + e6246fc8-9101-413c-a8e2-c2300b01aca8 + INFO + + + + + + + Shutdown container: <%= CHEF_SERVER_CONTAINER_NAME %> + Stop <%= CHEF_SERVER_CONTAINER_NAME %> + + chef_server-control + + e6246fc8-9101-413c-a8e2-c2300b01aca8 + + diff --git a/generate_scripts.rb b/generate_scripts.rb deleted file mode 100644 index a48c0c8..0000000 --- a/generate_scripts.rb +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/ruby -require 'erb' - -backend=ARGV[0] -start_ip=ARGV[1] -end_ip=ARGV[2] -gateway_ip=ARGV[3] -data_folder=ARGV[4] -mysql_password=ARGV[5] - -#Generate ENV file -template = ERB.new(File.read("templates/ENV.erb")) -xml_content = template.result(binding) -File.open("#{data_folder}/ENV", "w") do |file| - file.puts xml_content -end - -#Generate lib files -lib_files = ["generate_static_ip", "lists"] -lib_files.each do |file| - template = ERB.new(File.read("templates/#{file}.erb")) - xml_content = template.result(binding) - File.open("#{data_folder}/lib/#{file}.rb", "w") do |file| - file.puts xml_content - end -end - -#Generate misc files -misc_files = ["rundeck_jobs.xml", "get_images.rb"] -misc_files.each do |misc| - template = ERB.new(File.read("templates/#{misc}.erb")) - xml_content = template.result(binding) - File.open("#{data_folder}/#{misc}", "w") do |file| - file.puts xml_content - end -end - -system("sudo su rundeck -c 'rd-project -p cloud-control -a create'") -system("sudo su rundeck -c 'rd-jobs load -r -f #{data_folder}/rundeck_jobs.xml -p cloud-control'") - -#Generate user-data template files -user_data_templates=["TEMPLATE-user-data", "TEMPLATE-user-data-nat"].each do |ud| - ssh_key = File.read("templates/ssh_key").chomp - var = "<%= ip %>" - template = ERB.new(File.read("templates/#{ud}.erb")) - xml_content = template.result(binding) - File.open("#{data_folder}/templates/#{ud}.erb", "w") do |file| - file.puts xml_content - end -end - -system("sudo cp templates/TEMPLATE.xml.erb #{data_folder}/templates/") -system("sudo chown -R rundeck. #{data_folder}") - -puts "\nAll the scripts were generated!\n Cloud-Control folder location: #{data_folder}\n Backend: #{backend}\n Ip Range: #{start_ip} to #{end_ip}" - -if backend == "mysql" - begin - require 'mysql2' - client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => mysql_password) - client.query "CREATE DATABASE IF NOT EXISTS cloud_control CHARACTER SET utf8 COLLATE utf8_general_ci" - client.query "CREATE TABLE IF NOT EXISTS cloud_control.guests \ - (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(25), ip VARCHAR(17), vnc_port INT, network_type VARCHAR(8))" - rescue Mysql2::Error => e - puts e.error - end - puts "\nDatabase \"cloud_control\" created!" -elsif backend == "postgres" - begin - system("createdb -p 5432 -O pguser -U pguser -E UTF8 cloud_control") - require 'pg' - conn = PG::Connection.open(:dbname => "cloud_control", :user => "pguser") - conn.exec_params('CREATE TABLE IF NOT EXISTS guests ( - id serial PRIMARY KEY, name varchar(25) NOT NULL, ip varchar(17) NOT NULL, vnc_port int, network_type varchar(8))') - rescue PG::Error => e - puts e.error - end - puts "\nDatabase \"cloud_control\" created!" -end diff --git a/install b/install index 9c870c6..9286171 100755 --- a/install +++ b/install @@ -1,69 +1,14 @@ -#!/bin/bash - -backend="" -start_ip="" -end_ip="" -gateway_ip="" -data_folder="/srv/cloud-control" -mysql_password="cloudcontrol" -first_image_source="https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img" - -if [ -z "$backend" ]; then echo -e "Please set a backend type for the VMs' data\n\n Choose between: file, mysql OR postgres" && exit 1; fi -if [ -z "$start_ip" ] || [ -z "$end_ip" ]; then echo -e "Please set an IP Range!\n" && exit 1; fi -if [ -z "$gateway_ip" ]; then echo -e "Please set a gateway IP!\n" && exit 1; fi - -################## - -echo "System requirements" -sudo apt-get update -sudo apt-get -y install build-essential ruby1.9 qemu-utils cloud-utils kvm libvirt-bin +#!/bin/bash -e +echo "Welcome to:" +echo "-----------" +echo " __ __ __ __ __ ___ __ __" +echo " |\/| | /__\` /__\` | / \\ |\\ | / \` / \\ |\\ | | |__) / \\ |" +echo " | | | .__/ .__/ | \\__/ | \\| ___ \\__, \\__/ | \\| | | \\ \\__/ |___" +echo -e "\n\nInstallation is progress...\n" +sudo apt-get -q update +sudo apt-get -y install build-essential ruby1.9 echo "Installing: bundler" sudo gem install bundler --no-ri --no-rdoc bundle install -echo "Creating kvm_guests and sources folders" -sudo mkdir -p $data_folder/{kvm_guests,lib,templates,lists,sources/{iso,cloud_images}} -sudo chown -R rundeck. $data_folder -sudo cp lib/* $data_folder/lib -echo "Add rundeck user to libvirtd and kvm groups" -sudo adduser rundeck libvirtd && sudo adduser rundeck kvm -echo "Restart libvirtd..." -sudo service libvirt-bin restart -echo "Restart Rundeck...." -sudo sed -i s,"/var/lib/rundeck:/bin/false","/var/lib/rundeck:/bin/bash",g /etc/passwd -sudo service rundeckd restart -echo "Giving 20 seconds to Rundeck to fully restart, hang tight..." -sleep 20 - -if [[ $backend == "file" ]]; then - sudo mkdir $data_folder/interfaces - sudo touch $data_folder/interfaces/ip_address_pool.txt -elif [[ $backend == "mysql" ]]; then - echo "mysql-server-5.5 mysql-server/root_password password $mysql_password - mysql-server-5.5 mysql-server/root_password seen true - mysql-server-5.5 mysql-server/root_password_again password $mysql_password - mysql-server-5.5 mysql-server/root_password_again seen true - " | sudo debconf-set-selections - export DEBIAN_FRONTEND=noninteractive - sudo apt-get install -q -y mysql-server mysql-client libmysqlclient-dev - sudo gem install mysql2 -elif [[ $backend == "postgres" ]]; then - sudo apt-get -y install postgresql libpq-dev - sudo gem install pg - sudo su - postgres -c "createuser pguser -s" - echo -e "local all postgres peer\nlocal all pguser trust\nlocal all all peer\nhost all all 127.0.0.1/32 md5" | sudo tee /etc/postgresql/9.3/main/pg_hba.conf - sudo service postgresql restart -else - echo "Backend: ${backend} not supported!" - exit 1 -fi - ruby=`which ruby` -sudo $ruby generate_scripts.rb $backend $start_ip $end_ip $gateway_ip $data_folder $mysql_password - -sudo $ruby $data_folder/get_images.rb $first_image_source - -rundeck_url=`sudo cat /etc/rundeck/framework.properties |grep framework.server.url |awk '{print $3}'` -echo -e "\n#############" -echo "Cloud-Control installation completed!" -echo "Go to: ${rundeck_url}/project/cloud-control/jobs" -echo "######" +sudo $ruby scripts/menu_main.rb diff --git a/kvm/generate_scripts-floating.rb b/kvm/generate_scripts-floating.rb new file mode 100644 index 0000000..b9cadf3 --- /dev/null +++ b/kvm/generate_scripts-floating.rb @@ -0,0 +1,51 @@ +#!/usr/bin/ruby +require 'erb' + +def generate_scripts(backend, database_name, db_kvm_table, mysql_password, kvm_folder, start_ip, end_ip, gateway_ip, ssh_keys, floating) + dir=File.expand_path(File.dirname(__FILE__)) + + #Generate ENV file + template = ERB.new(File.read("#{dir}/templates/ENV.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/ENV", "w") do |file| + file.puts xml_content + end + + #Generate lib files + lib_files = ["mysql_backend", "postgres_backend", "generate_static_ip", "lists"] + lib_files.each do |file| + template = ERB.new(File.read("#{dir}/templates/#{file}.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/lib/#{file}.rb", "w") do |file| + file.puts xml_content + end + end + + #Generate misc files + misc_files = ["rundeck_jobs.xml", "get_images.rb"] + misc_files.each do |misc| + template = ERB.new(File.read("#{dir}/templates/#{misc}.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/#{misc}", "w") do |file| + file.puts xml_content + end + end + + system("sudo su rundeck -c 'rd-project -p kvm-control -a create'") + system("sudo su rundeck -c 'rd-jobs load -r -f #{kvm_folder}/rundeck_jobs.xml -p kvm-control'") + + #Generate user-data template files + user_data_templates=["TEMPLATE-user-data", "TEMPLATE-user-data-nat"].each do |ud| + var = "<%= ip %>" + template = ERB.new(File.read("#{dir}/templates/#{ud}.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/templates/#{ud}.erb", "w") do |file| + file.puts xml_content + end + end + + system("sudo cp #{dir}/templates/TEMPLATE.xml.erb #{kvm_folder}/templates/") + system("sudo chown -R rundeck. #{kvm_folder}") + + puts "\nAll the scripts were generated!\n Mission_Control folder location: #{kvm_folder}\n Backend: #{backend}\n Floating IP Range: #{start_ip} to #{end_ip}" +end diff --git a/kvm/generate_scripts.rb b/kvm/generate_scripts.rb new file mode 100644 index 0000000..980f434 --- /dev/null +++ b/kvm/generate_scripts.rb @@ -0,0 +1,49 @@ +#!/usr/bin/ruby +require 'erb' + +def generate_scripts(backend, database_name, db_kvm_table, mysql_password, kvm_folder, ssh_keys, floating) + dir=File.expand_path(File.dirname(__FILE__)) + + #Generate ENV file + template = ERB.new(File.read("#{dir}/templates/ENV.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/ENV", "w") do |file| + file.puts xml_content + end + + #Generate lib files + lib_files = ["mysql_backend", "postgres_backend", "generate_static_ip", "lists"] + lib_files.each do |file| + template = ERB.new(File.read("#{dir}/templates/#{file}.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/lib/#{file}.rb", "w") do |file| + file.puts xml_content + end + end + + #Generate misc files + misc_files = ["rundeck_jobs.xml", "get_images.rb"] + misc_files.each do |misc| + template = ERB.new(File.read("#{dir}/templates/#{misc}.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/#{misc}", "w") do |file| + file.puts xml_content + end + end + + system("sudo su rundeck -c 'rd-project -p kvm-control -a create'") + system("sudo su rundeck -c 'rd-jobs load -r -f #{kvm_folder}/rundeck_jobs.xml -p kvm-control'") + + #Generate user-data template file + var = "<%= ip %>" + template = ERB.new(File.read("#{dir}/templates/TEMPLATE-user-data-nat.erb")) + xml_content = template.result(binding) + File.open("#{kvm_folder}/templates/TEMPLATE-user-data-nat.erb", "w") do |file| + file.puts xml_content + end + + system("sudo cp #{dir}/templates/TEMPLATE.xml.erb #{kvm_folder}/templates/") + system("sudo chown -R rundeck. #{kvm_folder}") + + puts "\nAll the scripts were generated!\n Mission_Control folder location: #{kvm_folder}\n Backend: #{backend}" +end diff --git a/kvm/get_first_cloud_image.rb b/kvm/get_first_cloud_image.rb new file mode 100644 index 0000000..bcfec36 --- /dev/null +++ b/kvm/get_first_cloud_image.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby + +def get_first_cloud_image(kvm_folder, first_image_source) + filename = first_image_source.split(/\?/).first.split(/\//).last + if File.exist?(kvm_folder+"/sources/cloud_images/"+filename) + puts "#{filename} is already in #{kvm_folder}/sources/cloud_images/ skipping..." + else + system("sudo ruby #{kvm_folder}/get_images.rb #{first_image_source}") + end +end diff --git a/lib/def.rb b/kvm/lib/def.rb similarity index 100% rename from lib/def.rb rename to kvm/lib/def.rb diff --git a/kvm/setup_db.rb b/kvm/setup_db.rb new file mode 100644 index 0000000..3fc66d9 --- /dev/null +++ b/kvm/setup_db.rb @@ -0,0 +1,30 @@ +#!/usr/bin/ruby + +def setup_kvm_db(backend, database_name, db_kvm_table, mysql_password) + if backend == "mysql" + begin + bundle_install "mysql2" + Gem.clear_paths + require 'mysql2' + client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => mysql_password) + client.query "CREATE DATABASE IF NOT EXISTS #{database_name} CHARACTER SET utf8 COLLATE utf8_general_ci" + client.query "CREATE TABLE IF NOT EXISTS #{database_name}.#{db_kvm_table} \ + (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(25), ip VARCHAR(17), vnc_port INT, network_type VARCHAR(8), chef_installed VARCHAR(3))" + rescue Mysql2::Error => e + puts e.error + end + puts "\nDatabase \"#{database_name}\" created!" + elsif backend == "postgres" + begin + system("createdb -p 5432 -O pguser -U pguser -E UTF8 #{database_name}") + bundle_install "pg" + require 'pg' + conn = PG::Connection.open(:dbname => "#{database_name}", :user => "pguser") + conn.exec_params("CREATE TABLE IF NOT EXISTS #{db_kvm_table} ( + id serial PRIMARY KEY, name varchar(25) NOT NULL, ip varchar(17) NOT NULL, vnc_port int, network_type varchar(8), chef_installed VARCHAR(3))") + rescue PG::Error => e + puts e.error + end + puts "\nDatabase \"#{database_name}\" created!" + end +end diff --git a/templates/ENV.erb b/kvm/templates/ENV.erb similarity index 82% rename from templates/ENV.erb rename to kvm/templates/ENV.erb index 550d5f1..e71c55a 100644 --- a/templates/ENV.erb +++ b/kvm/templates/ENV.erb @@ -9,7 +9,7 @@ require 'fileutils' <% if backend == "mysql" %>require 'mysql2'<% elsif backend == "postgres" %>require 'pg'<% end %> #KVM guest -DATA_DIR="<%= data_folder %>" +DATA_DIR="<%= kvm_folder %>" GUESTS_DIR=DATA_DIR+"/kvm_guests" GUEST=GUESTS_DIR+"/"+guest XML=GUEST+"/"+guest+".xml" @@ -23,11 +23,11 @@ SOURCES=DATA_DIR+"/sources" ISO=SOURCES+"/iso" CLOUD_IMAGES=SOURCES+"/cloud_images" TEMPLATES=DATA_DIR+"/templates" -<% if backend == "mysql" %>MYSQL_PASSWORD="<%= mysql_password %>"<% elsif backend == "file" %>IP_FILE=DATA_DIR+"/interfaces/ip_address_pool.txt"<% end %> +<% if backend == "mysql" %>MYSQL_PASSWORD="<%= mysql_password %>"<% end %> -#Floating IP range +<% if floating == "yes" %>#Floating IP range START_IP="<%= start_ip %>" -END_IP="<%= end_ip %>" +END_IP="<%= end_ip %>"<% end %> #NAT IP range NAT_START_IP="192.168.122.2" diff --git a/templates/TEMPLATE-user-data-nat.erb b/kvm/templates/TEMPLATE-user-data-nat.erb similarity index 96% rename from templates/TEMPLATE-user-data-nat.erb rename to kvm/templates/TEMPLATE-user-data-nat.erb index 62bae0e..45e1179 100644 --- a/templates/TEMPLATE-user-data-nat.erb +++ b/kvm/templates/TEMPLATE-user-data-nat.erb @@ -6,7 +6,7 @@ package_update: true packages: - htop ssh_authorized_keys: - - <%= ssh_key %> + - <%= ssh_keys.to_a.join("\n - ") %> write_files: - content: | # The loopback network interface diff --git a/templates/TEMPLATE-user-data.erb b/kvm/templates/TEMPLATE-user-data.erb similarity index 96% rename from templates/TEMPLATE-user-data.erb rename to kvm/templates/TEMPLATE-user-data.erb index e43b933..62f255f 100644 --- a/templates/TEMPLATE-user-data.erb +++ b/kvm/templates/TEMPLATE-user-data.erb @@ -6,7 +6,7 @@ package_update: true packages: - htop ssh_authorized_keys: - - <%= ssh_key %> + - <%= ssh_keys.to_a.join("\n - ") %> write_files: - content: | # The loopback network interface diff --git a/templates/TEMPLATE.xml.erb b/kvm/templates/TEMPLATE.xml.erb similarity index 100% rename from templates/TEMPLATE.xml.erb rename to kvm/templates/TEMPLATE.xml.erb diff --git a/templates/generate_static_ip.erb b/kvm/templates/generate_static_ip.erb similarity index 80% rename from templates/generate_static_ip.erb rename to kvm/templates/generate_static_ip.erb index 2d9420a..c2010f1 100644 --- a/templates/generate_static_ip.erb +++ b/kvm/templates/generate_static_ip.erb @@ -18,10 +18,7 @@ def generate_ip (name, network_type) exit 1 end - <% if backend == "file" %> - file = File.open(IP_FILE).each do |lines| - ip_in_use << lines.tr("\n","") - end<% elsif backend == "mysql" %> + <% if backend == "mysql" %> require_relative "#{LIB}/mysql_backend.rb" ip_in_use = get_ips_list<% elsif backend == "postgres" %> require_relative "#{LIB}/postgres_backend.rb" @@ -54,9 +51,5 @@ def generate_ip (name, network_type) File.open("#{USER_DATA}/#{name}-user-data.erb", "w") do |file| file.puts interfaces end - <% if backend == "file" %> - File.open(IP_FILE, "a") {|f| - f << "#{available[0]}\n" - } - File.chmod(0664, IP_FILE)<% elsif backend == "mysql" || backend == "postgres" %>return ip<% end %> + <% if backend == "mysql" || backend == "postgres" %>return ip<% end %> end diff --git a/templates/get_images.rb.erb b/kvm/templates/get_images.rb.erb similarity index 90% rename from templates/get_images.rb.erb rename to kvm/templates/get_images.rb.erb index 22b53aa..6872b37 100755 --- a/templates/get_images.rb.erb +++ b/kvm/templates/get_images.rb.erb @@ -1,7 +1,7 @@ #!/usr/bin/ruby guest="" -eval File.read("<%= data_folder %>/ENV") +eval File.read("<%= kvm_folder %>/ENV") require_relative "#{LIB}/lists.rb" if ARGV[0].nil? @@ -30,5 +30,5 @@ else end add_to_list "source", filename, type - system("chown rundeck. <%= data_folder %>/lists/source_list.json") + system("chown rundeck. <%= kvm_folder %>/lists/source_list.json") puts "Download completed!".cyan diff --git a/templates/lists.erb b/kvm/templates/lists.erb similarity index 79% rename from templates/lists.erb rename to kvm/templates/lists.erb index 20d364e..4e13aee 100644 --- a/templates/lists.erb +++ b/kvm/templates/lists.erb @@ -5,9 +5,9 @@ require 'json' def add_to_list(type, attr, misc) if type == "vm" - list="<%= data_folder %>/lists/vm_list.json" + list="<%= kvm_folder %>/lists/vm_list.json" elsif type == "source" - list="<%= data_folder %>/lists/source_list.json" + list="<%= kvm_folder %>/lists/source_list.json" else puts "Unsupported type!".red exit 1 @@ -39,9 +39,9 @@ end def delete_from_list(type, attr, misc) if type == "vm" - list="<%= data_folder %>/lists/vm_list.json" + list="<%= kvm_folder %>/lists/vm_list.json" elsif type == "source" - list="<%= data_folder %>/lists/source_list.json" + list="<%= kvm_folder %>/lists/source_list.json" else puts "Unsupported type!".red exit 1 diff --git a/lib/mysql_backend.rb b/kvm/templates/mysql_backend.erb similarity index 61% rename from lib/mysql_backend.rb rename to kvm/templates/mysql_backend.erb index 4c22ae3..14b9f11 100644 --- a/lib/mysql_backend.rb +++ b/kvm/templates/mysql_backend.erb @@ -1,9 +1,9 @@ #!/usr/bin/ruby -def add_guest(name, ip, vnc_port, network_type) +def add_guest(name, ip, vnc_port, network_type, chef) begin client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => MYSQL_PASSWORD) - client.query "INSERT INTO cloud_control.guests (name, ip, vnc_port, network_type) VALUES ('#{name}', '#{ip}', '#{vnc_port}', '#{network_type}')" + client.query "INSERT INTO <%= database_name %>.<%= db_kvm_table %> (name, ip, vnc_port, network_type, chef_installed) VALUES ('#{name}', '#{ip}', '#{vnc_port}', '#{network_type}', '#{chef}')" rescue Mysql2::Error => e puts e.error end @@ -13,7 +13,7 @@ def get_ips_list begin ip_in_use=[] client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => MYSQL_PASSWORD) - client.query("SELECT * FROM cloud_control.guests").each do |row| + client.query("SELECT * FROM <%= database_name %>.<%= db_kvm_table %>").each do |row| ip_in_use << row["ip"] end rescue Mysql2::Error => e @@ -25,7 +25,7 @@ def get_ips_list def remove_guest(name) begin client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => MYSQL_PASSWORD) - client.query "DELETE FROM cloud_control.guests WHERE name = '#{name}'" + client.query "DELETE FROM <%= database_name %>.<%= db_kvm_table %> WHERE name = '#{name}'" rescue Mysql2::Error => e puts e.error end @@ -35,7 +35,7 @@ def guest_ip(name) begin ip = "" client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => MYSQL_PASSWORD) - client.query("SELECT * FROM cloud_control.guests WHERE name = '#{name}'").each do |x| + client.query("SELECT * FROM <%= database_name %>.<%= db_kvm_table %> WHERE name = '#{name}'").each do |x| ip = x["ip"] end rescue Mysql2::Error => e diff --git a/kvm/templates/postgres_backend.erb b/kvm/templates/postgres_backend.erb new file mode 100644 index 0000000..901239b --- /dev/null +++ b/kvm/templates/postgres_backend.erb @@ -0,0 +1,45 @@ +#!/usr/bin/ruby + +def add_guest(name, ip, vnc_port, network_type, chef) + begin + conn = PG::Connection.open(:dbname => "<%= database_name %>", :user => "pguser") + conn.exec_params "INSERT INTO <%= db_kvm_table %> (name, ip, vnc_port, network_type, chef_installed) VALUES ('#{name}', '#{ip}', '#{vnc_port}', '#{network_type}', '#{chef}')" + rescue PG::Error => e + puts e.error + end +end + +def get_ips_list + begin + ip_in_use=[] + conn = PG::Connection.open(:dbname => "<%= database_name %>", :user => "pguser") + conn.exec_params("SELECT * FROM <%= db_kvm_table %>").each do |row| + ip_in_use << row["ip"] + end + rescue PG::Error => e + puts e.error + end +return ip_in_use +end + +def remove_guest(name) + begin + conn = PG::Connection.open(:dbname => "<%= database_name %>", :user => "pguser") + conn.exec_params "DELETE FROM <%= db_kvm_table %> WHERE name = '#{name}'" + rescue PG::Error => e + puts e.error + end +end + +def guest_ip(name) + begin + ip = "" + conn = PG::Connection.open(:dbname => "<%= database_name %>", :user => "pguser") + conn.exec_params("SELECT * FROM <%= db_kvm_table %> WHERE name = '#{name}'").each do |x| + ip = x["ip"] + end + rescue PG::Error => e + puts e.error + end +return ip +end diff --git a/templates/rundeck_jobs.xml.erb b/kvm/templates/rundeck_jobs.xml.erb similarity index 91% rename from templates/rundeck_jobs.xml.erb rename to kvm/templates/rundeck_jobs.xml.erb index aa9c3d6..cc9f679 100644 --- a/templates/rundeck_jobs.xml.erb +++ b/kvm/templates/rundeck_jobs.xml.erb @@ -10,7 +10,7 @@ + <% if floating == "yes" %> + <% elsif floating == "no" %><% end %> - @@ -197,7 +199,7 @@ puts "New XML file created at: " + XML.underline]]> - @@ -251,7 +253,7 @@ puts ""]]> 20 - <% if backend == "file" %> @@ -343,17 +339,19 @@ puts "Carry on..."]]> + <% if floating == "yes" %> + <% elsif floating == "no" %><% end %> - @@ -369,7 +367,7 @@ puts "Carry on..."]]>