vagrant-cloudcenter 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,117 @@
1
+ module VagrantPlugins
2
+ module Cloudcenter
3
+ module Command
4
+ class Catalog < Vagrant.plugin("2", :command)
5
+ def self.synopsis
6
+ "Retrieve available catalog items"
7
+ end
8
+
9
+ def execute
10
+
11
+ access_key = ""
12
+ username = ""
13
+ host_ip = ""
14
+
15
+ if File.exists?("VagrantFile")
16
+ File.readlines("VagrantFile").grep(/cloudcenter.access_key/) do |line|
17
+ unless line.empty?
18
+ access_key = line.scan(/["']([^"']*)["']/)[0][0]
19
+ end
20
+ end
21
+ File.readlines("VagrantFile").grep(/cloudcenter.username/) do |line|
22
+ unless line.empty?
23
+ username = line.scan(/["']([^"']*)["']/)[0][0]
24
+
25
+ end
26
+ end
27
+ File.readlines("VagrantFile").grep(/cloudcenter.host_ip/) do |line|
28
+ unless line.empty?
29
+ host_ip = line.scan(/["']([^"']*)["']/)[0][0]
30
+
31
+ end
32
+ end
33
+ end
34
+
35
+ if access_key.empty?
36
+ access_key = ENV["access_key"]
37
+ end
38
+ if username.empty?
39
+ username = ENV["username"]
40
+ end
41
+ if host_ip.empty?
42
+ host_ip = ENV["host_ip"]
43
+ end
44
+
45
+ if access_key.nil?
46
+ puts "Access Key missing. Please enter into VagrantFile or environmental variable 'export access_key= '"
47
+ end
48
+ if username.nil?
49
+ puts "Username missing. Please enter into VagrantFile or environmental variable 'export username= '"
50
+ end
51
+ if host_ip.nil?
52
+ puts "Host IP missing. Please enter into VagrantFile or environmental variable 'export host_ip= '"
53
+ end
54
+
55
+ if !(access_key.nil? or username.nil? or host_ip.nil?)
56
+ begin
57
+
58
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v1/apps");
59
+
60
+ catalog = JSON.parse(RestClient::Request.execute(
61
+ :method => :get,
62
+ :url => encoded,
63
+ :verify_ssl => false,
64
+ :content_type => "json",
65
+ :accept => "json"
66
+ ));
67
+
68
+ catalogs = catalog["apps"]
69
+
70
+
71
+ # build table with data returned from above function
72
+
73
+ table = Text::Table.new
74
+ table.head = ["ID", "Name", "Versions", "Category"]
75
+ puts "\n"
76
+
77
+ #for each item in returned list, display certain attributes in the table
78
+ catalogs.each do |row|
79
+ id = row["serviceTierId"]
80
+ appName = row["name"]
81
+ description = row["description"]
82
+ versions = row["versions"]
83
+ category = row["category"]
84
+
85
+ table.rows << ["#{id}","#{appName}", "#{versions}", "#{category}"]
86
+ end
87
+
88
+ puts table
89
+
90
+ puts"\n"
91
+
92
+ rescue => e
93
+
94
+ if e.inspect == "Timed out connecting to server"
95
+ puts "\n#ConnectionError - Unable to connnect to CloudCenter Manager \n"
96
+ exit
97
+ else
98
+ error = JSON.parse(e.response)
99
+ code = error["errors"][0]["code"]
100
+
101
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
102
+ puts "\n #{error['errors'][0]['message']}\n\n"
103
+
104
+ exit
105
+ end
106
+ end
107
+ end
108
+
109
+ 0
110
+
111
+
112
+
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,50 @@
1
+ module VagrantPlugins
2
+ module Cloudcenter
3
+ module Command
4
+ class Init < Vagrant.plugin("2", :command)
5
+ def self.synopsis
6
+ "Create inital Vagrant file"
7
+ end
8
+
9
+ # This will build the Vagrantfile and insert the attributes required
10
+
11
+
12
+ def createInitFile()
13
+
14
+ script = File.open("Vagrantfile", "w")
15
+
16
+ script.puts "# -*- mode: ruby -*-
17
+ # vi: set ft=ruby :
18
+
19
+ Vagrant.configure(2) do |config|
20
+
21
+ config.vm.box = 'cloudcenter'
22
+
23
+ config.ssh.private_key_path = ['/Users/MYUSERNAME/.ssh/id_rsa','/Users/MYUSERNAME/.vagrant.d/insecure_private_key']
24
+ config.ssh.insert_key = false
25
+
26
+ config.vm.provider :cloudcenter do |cloudcenter|
27
+ cloudcenter.username = 'my_username'
28
+ cloudcenter.access_key = 'my_access_key'
29
+ cloudcenter.host_ip = 'cloudcenter_host_ip_address'
30
+ cloudcenter.deployment_config = 'sample_deployment_config.json'
31
+ end
32
+
33
+ config.vm.synced_folder '.', '/opt/my_files/', type: 'rsync'
34
+
35
+ end"
36
+ script.close
37
+
38
+ end
39
+
40
+
41
+
42
+ def execute
43
+
44
+ createInitFile()
45
+
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,121 @@
1
+ module VagrantPlugins
2
+ module Cloudcenter
3
+ module Command
4
+ class Jobs < Vagrant.plugin("2", :command)
5
+ def self.synopsis
6
+ "Retrieve available jobs"
7
+ end
8
+
9
+ def execute
10
+
11
+ access_key = ""
12
+ username = ""
13
+ host_ip = ""
14
+
15
+
16
+ if File.exists?("VagrantFile")
17
+ File.readlines("VagrantFile").grep(/cloudcenter.access_key/) do |line|
18
+ unless line.empty?
19
+ access_key = line.scan(/["']([^"']*)["']/)[0][0]
20
+ end
21
+ end
22
+ File.readlines("VagrantFile").grep(/cloudcenter.username/) do |line|
23
+ unless line.empty?
24
+ username = line.scan(/["']([^"']*)["']/)[0][0]
25
+
26
+ end
27
+ end
28
+ File.readlines("VagrantFile").grep(/cloudcenter.host_ip/) do |line|
29
+ unless line.empty?
30
+ host_ip = line.scan(/["']([^"']*)["']/)[0][0]
31
+
32
+ end
33
+ end
34
+ end
35
+
36
+ if access_key.empty?
37
+ access_key = ENV["access_key"]
38
+ end
39
+ if username.empty?
40
+ username = ENV["username"]
41
+ end
42
+ if host_ip.empty?
43
+ host_ip = ENV["host_ip"]
44
+ end
45
+
46
+
47
+ if access_key.nil?
48
+ puts "Access Key missing. Please enter into VagrantFile or environmental variable 'export access_key= '"
49
+ end
50
+ if username.nil?
51
+ puts "Username missing. Please enter into VagrantFile or environmental variable 'export username= '"
52
+ end
53
+ if host_ip.nil?
54
+ puts "Host IP missing. Please enter into VagrantFile or environmental variable 'export host_ip= '"
55
+ end
56
+
57
+ if !(access_key.nil? or username.nil? or host_ip.nil?)
58
+ begin
59
+
60
+ encoded = URI.encode("https://#{username}:#{access_key}@#{host_ip}/v2/jobs");
61
+
62
+ catalog = JSON.parse(RestClient::Request.execute(
63
+ :method => :get,
64
+ :url => encoded,
65
+ :verify_ssl => false,
66
+ :content_type => "json",
67
+ :accept => "json"
68
+ ));
69
+
70
+ catalogs = catalog["jobs"]
71
+
72
+
73
+ # build table with data returned from above function
74
+
75
+ table = Text::Table.new
76
+ table.head = ["ID", "Name", "Family", "Owner","Hours","Status"]
77
+ puts "\n"
78
+
79
+ #for each item in returned list, display certain attributes in the table
80
+ catalogs.each do |row|
81
+ id = row["id"]
82
+ appName = row["name"]
83
+ family = row["cloudFamily"]
84
+ owner = row["ownerEmailAddress"]
85
+ hours = row["nodeHours"]
86
+ status = row["status"]
87
+ statusMessage = row["jobStatusMessage"]
88
+
89
+ table.rows << ["#{id}","#{appName}", "#{family}", "#{owner}", "#{hours}", "#{status}"]
90
+ end
91
+
92
+ puts table
93
+
94
+ puts"\n"
95
+
96
+ rescue => e
97
+
98
+ if e.inspect == "Timed out connecting to server"
99
+ puts "\n#ConnectionError - Unable to connnect to CloudCenter Manager \n"
100
+ exit
101
+ else
102
+ error = JSON.parse(e.response)
103
+ code = error["errors"][0]["code"]
104
+
105
+ puts "\n Error code: #{error['errors'][0]['code']}\n"
106
+ puts "\n #{error['errors'][0]['message']}\n\n"
107
+
108
+ exit
109
+ end
110
+ end
111
+ end
112
+
113
+ 0
114
+
115
+
116
+
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,94 @@
1
+ require 'vagrant-cloudcenter/action'
2
+ require 'highline/import'
3
+ require 'colorize'
4
+ require 'json'
5
+ require 'text-table'
6
+ require 'rest-client'
7
+
8
+ module VagrantPlugins
9
+ module Cloudcenter
10
+ module Command
11
+ class Root < Vagrant.plugin("2", :command)
12
+ def self.synopsis
13
+ "deploy a new environment using Cisco CloudCenter"
14
+ end
15
+
16
+ def initialize(argv, env)
17
+ @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
18
+
19
+ @subcommands = Vagrant::Registry.new
20
+ @subcommands.register(:catalog) do
21
+ require File.expand_path("../catalog", __FILE__)
22
+ Catalog
23
+ end
24
+ @subcommands.register(:app) do
25
+ require File.expand_path("../app", __FILE__)
26
+ App
27
+ end
28
+ @subcommands.register(:jobs) do
29
+ require File.expand_path("../jobs", __FILE__)
30
+ Jobs
31
+ end
32
+ @subcommands.register(:init) do
33
+ require File.expand_path("../init", __FILE__)
34
+ Init
35
+ end
36
+ @subcommands.register(:sync) do
37
+ require File.expand_path("../sync", __FILE__)
38
+ Sync
39
+ end
40
+
41
+ super(argv, env)
42
+ end
43
+
44
+ def execute
45
+ if @main_args.include?("-h") || @main_args.include?("--help")
46
+ # Print the help for all the commands.
47
+ return help
48
+ end
49
+
50
+ command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
51
+ return help if !command_class || !@sub_command
52
+ @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
53
+
54
+ # Initialize and execute the command class
55
+ command_class.new(@sub_args, @env).execute
56
+ end
57
+
58
+ def help
59
+ opts = OptionParser.new do |opts|
60
+ opts.banner = "Usage: vagrant cloudcenter <subcommand> [<args>]"
61
+ opts.separator ""
62
+ opts.separator "Available subcommands:\n"
63
+
64
+ # Add the available subcommands as separators in order to print them
65
+ # out as well.
66
+ keys = []
67
+ commands = {}
68
+ longest = 0
69
+
70
+ @subcommands.each do |key, data|
71
+
72
+ keys << key
73
+ commands[key] = data.synopsis
74
+ longest = key.length if key.length > longest
75
+
76
+ end
77
+
78
+ keys.sort.each do |key|
79
+ key.to_sym
80
+ synopsis = commands[key].to_str
81
+ command = key.to_s
82
+ opts.separator " #{command.ljust(longest+2)} #{synopsis}"
83
+ end
84
+
85
+ opts.separator ""
86
+ opts.separator "For help on any individual subcommand run `vagrant cloudcenter <subcommand> -h`"
87
+ end
88
+
89
+ @env.ui.info(opts.help, :prefix => false)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,29 @@
1
+ require "pathname"
2
+ require "vagrant/action/builder"
3
+
4
+
5
+ module VagrantPlugins
6
+ module Cloudcenter
7
+ module Command
8
+
9
+ include Vagrant::Action::Builtin
10
+
11
+ class Sync < Vagrant.plugin("2", :command)
12
+
13
+ def self.synopsis
14
+ "Sync files from host to guest"
15
+ end
16
+
17
+ def execute
18
+
19
+ with_target_vms() do |machine|
20
+ machine.action(:sync)
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module Cloudcenter
5
+ class Config < Vagrant.plugin("2", :config)
6
+ # The access key ID for accessing Cloudcenter.
7
+ #
8
+ # @return [String]
9
+ attr_accessor :access_key
10
+
11
+ # The Catalog Name for the VM to be provisioned from
12
+ #
13
+ # @return [String]
14
+ attr_accessor :host_ip
15
+
16
+ # Comment to use when provisioning the VM
17
+ #
18
+ # @return [String]
19
+ attr_accessor :username
20
+
21
+ # JSON config representing the environment to be deployed
22
+ #
23
+ # @return [String]
24
+ attr_accessor :deployment_config
25
+
26
+
27
+ def initialize()
28
+ @access_key = UNSET_VALUE
29
+ @host_ip = UNSET_VALUE
30
+ @host_port = UNSET_VALUE
31
+ @username = UNSET_VALUE
32
+ @deployment_config = UNSET_VALUE
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module Cloudcenter
5
+ module Errors
6
+ class VagrantCloudcenterError < Vagrant::Errors::VagrantError
7
+ error_namespace("vagrant_cloudcenter.errors")
8
+ end
9
+
10
+ class FogError < VagrantAWSError
11
+ error_key(:fog_error)
12
+ end
13
+
14
+ class InternalFogError < VagrantAWSError
15
+ error_key(:internal_fog_error)
16
+ end
17
+
18
+ class InstanceReadyTimeout < VagrantAWSError
19
+ error_key(:instance_ready_timeout)
20
+ end
21
+
22
+ class InstancePackageError < VagrantAWSError
23
+ error_key(:instance_package_error)
24
+ end
25
+
26
+ class InstancePackageTimeout < VagrantAWSError
27
+ error_key(:instance_package_timeout)
28
+ end
29
+
30
+ class RsyncError < VagrantAWSError
31
+ error_key(:rsync_error)
32
+ end
33
+
34
+ class MkdirError < VagrantAWSError
35
+ error_key(:mkdir_error)
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,74 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "The Vagrant CloudCenter plugin must be run within Vagrant."
5
+ end
6
+
7
+ module VagrantPlugins
8
+ module Cloudcenter
9
+ class Plugin < Vagrant.plugin("2")
10
+ name "vagrant-cloudcenter"
11
+ description <<-DESC
12
+ This plugin installs a provider that allows Vagrant to manage
13
+ machines in Cisco Cloud Center
14
+ DESC
15
+
16
+ config(:cloudcenter, :provider) do
17
+ require_relative "config"
18
+ Config
19
+ end
20
+
21
+ provider(:cloudcenter) do
22
+ # Setup logging and i18n
23
+ setup_logging
24
+ setup_i18n
25
+
26
+ # Return the provider
27
+ require_relative "provider"
28
+ Provider
29
+ end
30
+
31
+ command('cloudcenter') do
32
+ require_relative "command/root"
33
+ Command::Root
34
+ end
35
+
36
+
37
+
38
+ # This initializes the internationalization strings.
39
+ def self.setup_i18n
40
+ I18n.load_path << File.expand_path("../../../locales/en.yml", __FILE__)
41
+ I18n.reload!
42
+ end
43
+
44
+ # This sets up our log level to be whatever VAGRANT_LOG is.
45
+ def self.setup_logging
46
+ require "log4r"
47
+
48
+ level = nil
49
+ begin
50
+ level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
51
+ rescue NameError
52
+ # This means that the logging constant wasn't found,
53
+ # which is fine. We just keep `level` as `nil`. But
54
+ # we tell the user.
55
+ level = nil
56
+ end
57
+
58
+ # Some constants, such as "true" resolve to booleans, so the
59
+ # above error checking doesn't catch it. This will check to make
60
+ # sure that the log level is an integer, as Log4r requires.
61
+ level = nil if !level.is_a?(Integer)
62
+
63
+ # Set the logging level on all "vagrant" namespaced
64
+ # logs as long as we have a valid level.
65
+ if level
66
+ logger = Log4r::Logger.new("cloudcenter")
67
+ logger.outputters = Log4r::Outputter.stderr
68
+ logger.level = level
69
+ logger = nil
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,51 @@
1
+ require "log4r"
2
+ require "vagrant"
3
+ require "vagrant-cloudcenter/action"
4
+
5
+ module VagrantPlugins
6
+ module Cloudcenter
7
+ class Provider < Vagrant.plugin("2", :provider)
8
+ def initialize(machine)
9
+ @machine = machine
10
+ end
11
+
12
+ def action(name)
13
+ # Attempt to get the action method from the Action class if it
14
+ # exists, otherwise return nil to show that we don't support the
15
+ # given action.
16
+ action_method = "action_#{name}"
17
+ return Action.send(action_method) if Action.respond_to?(action_method)
18
+ nil
19
+ end
20
+
21
+ def ssh_info
22
+ # Run a custom action called "read_ssh_info" which does what it
23
+ # says and puts the resulting SSH info into the `:machine_ssh_info`
24
+ # key in the environment.
25
+ env = @machine.action("read_ssh_info")
26
+ env[:machine_ssh_info]
27
+ end
28
+
29
+ def state
30
+ # Run a custom action we define called "read_state" which does
31
+ # what it says. It puts the state in the `:machine_state_id`
32
+ # key in the environment.
33
+ env = @machine.action("read_state")
34
+
35
+ state_id = env[:machine_state_id]
36
+
37
+ # Get the short and long description
38
+ short = I18n.t("cloudcenter.states.short_#{state_id}")
39
+ long = I18n.t("cloudcenter.states.long_#{state_id}")
40
+
41
+ # Return the MachineState object
42
+ Vagrant::MachineState.new(state_id, short, long)
43
+ end
44
+
45
+ def to_s
46
+ id = @machine.id.nil? ? "new" : @machine.id
47
+ "cloudcenter (#{id})"
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module Cloudcenter
3
+ module Util
4
+ class Timer
5
+ # A basic utility method that times the execution of the given
6
+ # block and returns it.
7
+ def self.time
8
+ start_time = Time.now.to_f
9
+ yield
10
+ end_time = Time.now.to_f
11
+
12
+ end_time - start_time
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Cloudcenter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "pathname"
2
+
3
+ require 'vagrant-cloudcenter/plugin'
4
+
5
+ module VagrantPlugins
6
+ module Cloudcenter
7
+ lib_path = Pathname.new(File.expand_path("../vagrant-cloudcenter", __FILE__))
8
+ autoload :Action, lib_path.join("action")
9
+ autoload :Errors, lib_path.join("errors")
10
+
11
+ # This returns the path to the source of this plugin.
12
+ #
13
+ # @return [Pathname]
14
+ def self.source_root
15
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
16
+ end
17
+ end
18
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,33 @@
1
+ en:
2
+ cloudcenter:
3
+ already_created:
4
+ The machine is already created.
5
+ already_running:
6
+ The machine is already running.
7
+ already_stopped:
8
+ The machine is already stopped.
9
+ deploying:
10
+ VM is currently being deployed.
11
+ not_created:
12
+ Instance is not created. Please run `vagrant up` first.
13
+ waiting_for_ssh:
14
+ Waiting for SSH to become available...
15
+ ready:
16
+ Machine is booted and ready for use!
17
+ starting:
18
+ Starting the instance...
19
+ stopped:
20
+ Machine is now stopped...
21
+ terminated:
22
+ Machine has been removed...
23
+ stopping:
24
+ Stopping the instance...
25
+ terminating:
26
+ Terminating the instance...
27
+ waiting_for_ready:
28
+ Waiting for instance to become "ready"...
29
+ waiting_for_ssh:
30
+ Waiting for SSH to become available...
31
+ will_not_destroy: |-
32
+ The instance '%{name}' will not be destroyed, since the confirmation
33
+ was declined.