Module: Mccloud::Provider::Aws::VmCommand

Included in:
Vm
Defined in:
lib/mccloud/provider/aws/vm/up.rb,
lib/mccloud/provider/aws/vm/scp.rb,
lib/mccloud/provider/aws/vm/ssh.rb,
lib/mccloud/provider/aws/vm/halt.rb,
lib/mccloud/provider/aws/vm/rsync.rb,
lib/mccloud/provider/aws/vm/reload.rb,
lib/mccloud/provider/aws/vm/destroy.rb,
lib/mccloud/provider/aws/vm/forward.rb,
lib/mccloud/provider/aws/vm/package.rb,
lib/mccloud/provider/aws/vm/bootstrap.rb,
lib/mccloud/provider/aws/vm/provision.rb

Instance Method Summary collapse

Instance Method Details

#_bootstrap(command, options = nil) ⇒ Object



7
8
9
# File 'lib/mccloud/provider/aws/vm/bootstrap.rb', line 7

def _bootstrap(command,options=nil)
   ssh_bootstrap(command,options)
end

#_provision(options = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mccloud/provider/aws/vm/provision.rb', line 5

def _provision(options=nil)
  unless raw.nil?

    if raw.ready?
      self.share

      @provisioners.each do |provisioner|
        env.ui.info "[#{@name}] - starting provisioning with #{provisioner.name} as provisioner"
        provisioner.run(self)
      end
    else
      raise Mccloud::Error, "[#{@name}] - machine is not running, skipping provisioning"
    end
  else
    raise Mccloud::Error, "[#{@name}] - machine doesn't exit yet"
  end
end

#check_key(key_name) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/mccloud/provider/aws/vm/up.rb', line 47

def check_key(key_name)
  if @provider.check_keypairs
    raw_keypair=@provider.raw.key_pairs.get(key_name)
    if raw_keypair.nil?
          raise Mccloud::Error, "keypair #{key_name} does not exits. And we only managed keys with prefix mccloud"
      raise Mccloud::Error, "keypair #{key_name} does not exist"
    end
  end
end

#check_security_groups(groups) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mccloud/provider/aws/vm/up.rb', line 17

def check_security_groups(groups)
  if @provider.check_security_groups
    # Iterate over all groups
    groups.each do |group|

      if security_group_exists?(group)
        env.logger.info "security group #{group} exists"
      else
        env.logger.info "security group #{group} does not yet exist"
        if security_group_is_managed_by_mccloud?(group)
          # Managed by mccloud
          env.logger.info "security group #{group} starts with mccloud"
          env.ui.info "Creating security group #{group}"
          @provider.create_sg(group)
        else
          # Not managed by mccloud
          raise Mccloud::Error, "security group #{group} does not exits. And we only managed security groups with prefix mccloud"
        end
      end

      unless security_group_has_ssh_open?(group)
      end
    end
  end
end

#destroy(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mccloud/provider/aws/vm/destroy.rb', line 5

def destroy(options)

  unless raw.nil? || raw.state == "shutting-down" || raw.state =="terminated"
    env.ui.info "[#{@name}] - Destroying machine (#{raw.id})"
    raw.destroy

    raw.wait_for {  print "."; STDOUT.flush; state=="terminated"}
    env.ui.info ""
  else
    env.ui.info "[#{@name}] - Machine is already terminated #{@raw.id}"
  end

end

#forward(command, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/mccloud/provider/aws/vm/forward.rb', line 5

def forward(command,options={})
  @forward_threads=Array.new
  unless raw.nil?
    return self.ssh_forward(options)
  else
    env.ui.info "[#{self.name}] not yet created"
    return [] 
  end
end

#halt(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mccloud/provider/aws/vm/halt.rb', line 5

def halt(options)

  if self.running?
    env.ui.info "Halting machine #{@name}(#{@raw.id})"
    raw.stop
    raw.wait_for { printf "."; STDOUT.flush; state=="stopped"}
    env.ui.info ""
  else
    unless @raw.nil?
      env.ui.info "#{@name}(#{@raw.id}) is already halted."
    else
      env.ui.info "#{@name} is not yet created or was terminated"
    end
  end

end

#key_is_managed_by_mccloud?(keyname) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/mccloud/provider/aws/vm/up.rb', line 43

def key_is_managed_by_mccloud?(keyname)
    return /^mccloud/ =~ keyname
end

#package(options = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mccloud/provider/aws/vm/package.rb', line 5

def package(options=nil)

  #f=Fog::Compute.new({ :region => "eu-west-1", :provider => "AWS"})
  # i=f.create_image("i-c1ac2bb7","name","description")
  # f.images.all({ "Owner" => "self"})
  # f.deregister_image("ami-796d5b0d")

  time=Time.new
  strtime=time.strftime("%Y-%m-%d-%H-%M-%S")
  name=""

  if options["name"].nil?
    name="#{@name}-#{strtime}"
  else
    name=options["name"]
  end

  description=""
  if options["description"].nil?
    description="Autogenerated snapshot from machine #{@name}"
  else
    description=options["description"]
  end

  begin
    puts raw.id
    result=@provider.raw.create_image(raw.id,name,description)
    imageId=result.body["imageId"]
    env.ui.info "[#{@name}] image #{imageId} with #{description} being created"
    unless options["dontwait"]
      sleep 3
      counter=0
      max_tries=5
      #Pending
      state="pending"
      until state!="pending" do
        print "."
        sleep 2
        image=@provider.raw.images.get(imageId)

        if image.nil?
          sleep 3
          print "."
        else
          state=image.state
        end
      end
      env.ui.info ""
      env.ui.info "[#{@name}] creation done!"
    end
  rescue Fog::Service::Error => fogerror
    env.ui.error "[Error] #{fogerror}"

  end
end

#reload(options) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/mccloud/provider/aws/vm/reload.rb', line 4

def reload(options)
   env.logger.info "Checking if #{@name} is running: #{self.running?}"
  if self.running?
    self.halt(options)
  end
  self.up(options)
end

#rsync(src, dest, options) ⇒ Object



5
6
7
# File 'lib/mccloud/provider/aws/vm/rsync.rb', line 5

def rsync(src,dest,options)
  super(src,dest,options)
end

#sanitize(options) ⇒ Object

sanitize the options by converting to a hash with all keys converted to symbols as required by net/ssh



24
25
26
# File 'lib/mccloud/provider/aws/vm/scp.rb', line 24

def sanitize(options)
  Hash[options.map{|(k,v)| [k.to_sym,v]}]
end

#scp(local_path, remote_path, scp_options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/mccloud/provider/aws/vm/scp.rb', line 9

def scp(local_path, remote_path, scp_options = {})
  unless File.exists?(local_path)
    raise Mccloud::Error,"scp failed: #{local_path} does not exist"
  end

  #@raw.scp(src,dest)
  scp_options[:key_data] = [@raw.private_key] if @raw.private_key

  ::Fog::SCP.new(self.ip_address, @raw.username, sanitize(scp_options)).upload(local_path, remote_path, {})
end

#security_group_exists?(group) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mccloud/provider/aws/vm/up.rb', line 9

def security_group_exists?(group)
 return !@provider.raw.security_groups.get(group).nil?
end

#security_group_has_ssh_open?(group) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/mccloud/provider/aws/vm/up.rb', line 5

def security_group_has_ssh_open?(group)
 return false
end

#security_group_is_managed_by_mccloud?(group) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/mccloud/provider/aws/vm/up.rb', line 13

def security_group_is_managed_by_mccloud?(group)
    return /^mccloud/ =~ group
end

#ssh(command, options = {}) ⇒ Object



5
6
7
# File 'lib/mccloud/provider/aws/vm/ssh.rb', line 5

def ssh(command,options={})
  super(command,options)
end

#transfer(src, dest, options = {}) ⇒ Object



5
6
7
# File 'lib/mccloud/provider/aws/vm/scp.rb', line 5

def transfer(src,dest,options = {})
  scp(src,dest,options)
end

#up(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mccloud/provider/aws/vm/up.rb', line 57

def up(options={})

  if raw.nil? || raw.state =="terminated"

    create_options= {
      :private_key_path => @private_key_path ,
      :public_key_path => @public_key_path,
      :availability_zone => @zone,
      :image_id => @ami,
      :flavor_id => @flavor,
      :key_name => @key_name,
      :user_data => @user_data,
      :groups => @security_groups,
      :tags  => @tags
    }.merge(@create_options)

    # Always add the name tag
    create_options[:tags]["Name"] = "#{@provider.filter}#{@name}"

    check_security_groups(create_options[:groups])
    check_key(create_options[:key_name])

    env.ui.info "Creating new vm #{@name} for provider #{@provider.name}"
    begin
      @raw=@provider.raw.servers.create(create_options)
    rescue ::Fog::Compute::AWS::NotFound => ex
      raise ::Mccloud::Error, "Error creating the new server: #{ex}"
      return
    end

    env.ui.info "[#{@name}] - Waiting for the machine to become accessible"
    raw.wait_for { printf "."; STDOUT.flush;  ready?}
    env.ui.info ""

    # Wait for ssh to become available ...
    env.ui.info "[#{@name}] - Waiting for ssh port to become available"

    Mccloud::Util::Ssh.execute_when_tcp_available(self.ip_address, { :port => @port, :timeout => 6000 }) do
      env.ui.info "[#{@name}] - Ssh Port is available"
    end

    Mccloud::Util::Ssh.(self.ip_address, { :user => @user, :port => @port, :timeout => 6000 ,:keys => [@private_key_path]}) do
      env.ui.info "[#{@name}] - Ssh login works , proceeding with bootstrap"
    end
    # No bootstrap to provide
    self._bootstrap(nil,options)

  else
    state=raw.state
    if state =="stopped"
      env.ui.info "Booting up machine #{@name}"
      raw.start
      raw.wait_for { printf ".";STDOUT.flush;  ready?}
      env.ui.info ""
    else
      unless raw.state == "shutting-down" || raw.state =="terminated"
        env.ui.info "[#{@name}] - already running."
      else
        env.ui.info "[#{@name}] - can't start machine because it is in state #{raw.state}"
        return
      end
    end
  end

  unless options["noprovision"]
    env.ui.info "[#{@name}] - Waiting for ssh to become available"
    Mccloud::Util::Ssh.execute_when_tcp_available(self.ip_address, { :port => @port, :timeout => 6000 }) do
      env.ui.info "[#{@name}] - Ssh is available , proceeding with provisioning"
    end

    env.ui.info "[#{@name}] - provision step #{@name}"
    self._provision(options)
  end


end