Module: Mccloud::Provider::Aws::ProviderCommand

Included in:
Provider
Defined in:
lib/mccloud/provider/aws/provider/status.rb,
lib/mccloud/provider/aws/provider/ip_list.rb,
lib/mccloud/provider/aws/provider/lb_list.rb,
lib/mccloud/provider/aws/provider/image_list.rb,
lib/mccloud/provider/aws/provider/image_destroy.rb,
lib/mccloud/provider/aws/provider/keystore_list.rb,
lib/mccloud/provider/aws/provider/keystore_sync.rb

Instance Method Summary collapse

Instance Method Details

#image_destroy(imageId, options = nil) ⇒ Object



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

def image_destroy(imageId,options=nil)
    begin
      image=raw.images.get(imageId)
      if image.nil?
        env.ui.info "[#{name}] - ImageId #{imageId} not found"
      else
        env.ui.info "[#{name}] - ImageId #{imageId} found"
        env.ui.info "[#{name}] - Deregistering #{imageId} now"
        begin
          raw.deregister_image(imageId)
        rescue Fog::Service::Error => fogerror
          env.ui.error "[Error] #{fogerror}"
        end
      end
  end
end

#image_list(selection = nil, options = nil) ⇒ Object



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

def image_list(selection=nil,options=nil)
  images=raw.images.all({"Owner" => "self"})
  env.logger.info("#{selection} - #{options}")
  images.each do |image|
    require 'pp'
    env.ui.info "Id: #{image.id} Name: #{image.name}, Description: #{image.description}"
  end
end

#ip_list(selection = nil, options = nil) ⇒ Object



6
7
8
9
10
11
# File 'lib/mccloud/provider/aws/provider/ip_list.rb', line 6

def ip_list(selection=nil,options=nil)
  env.logger.info("#{selection} - #{options}")
  raw.addresses.each do |address|
    env.ui.info "Ip-address #{address.public_ip} - Server-Id #{address.server_id}"
  end
end

#keystore_list(selection = nil, options = nil) ⇒ Object



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

def keystore_list(selection=nil,options=nil)

  if raw.key_pairs.empty?
    env.ui.info("No Keypairs found")
  else
    raw.key_pairs.each do |keypair|
      env.ui.info "KeyPair #{keypair.name} - #{keypair.fingerprint}"
    end
  end

end

#keystore_sync(selection = nil, options = nil) ⇒ Object



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
# File 'lib/mccloud/provider/aws/provider/keystore_sync.rb', line 8

def keystore_sync(selection=nil,options=nil)

  env.config.keystores.each do |name,store|
    if store.keypairs.empty?
      env.ui.info "No Keypairs specified for keystore '#{name}'"
    else

      selected_keypairs=store.keypairs

      selected_keypairs.each do |defined_pair|
        keypair=env.config.keypairs[defined_pair[:keypair]]
        remote_name=defined_pair[:name]
        if keypair.nil?
          env.ui.error "Keypair #{defined_pair[:keypair]} is not defined"
        else
          name=keypair.name
          begin
            # Read key file
            key=File.read(keypair.public_key_path)

            # Check if key already exists
            existing_key=raw.key_pairs.get(remote_name)
            if existing_key.nil?
              # It does not exist, just create it
              env.ui.info "Creating Remote Key #{remote_name}"
              raw.key_pairs.create(:name => remote_name, :public_key => key)
            else
              if options.has_key?("overwrite")
                # Exists but overwrite was specified
                env.ui.info "Remote key '#{remote_name}' exists but --overwrite specified, removing key first"
                existing_key.destroy
                env.ui.info "Creating Remote Key #{remote_name}"
                raw.key_pairs.create(:name => defined_pair[:name], :public_key => key)
              else
                # Exists but overwrite was NOT specified
                env.ui.info "Remote Key '#{remote_name}' already exists. Use 'mccloud sync --overwrite'"
              end
            end
          rescue Errno::ENOENT => ex
            env.ui.error "Error: public_key_path does not exist : #{keypair.public_key_path}"
          rescue Error => ex
            env.ui.error "Error uploading key : #{ex}"
          end
        end
      end
    end

  end
end

#lb_list(selection = nil, options = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/mccloud/provider/aws/provider/lb_list.rb', line 6

def lb_list(selection=nil,options=nil)
  env.logger.info("#{selection} - #{options}")

  env.logger.info("Looking for loadbalancers in region #{@region}")
  ::Fog.credential = @credential
  elb=::Fog::AWS::ELB.new({:region => @region}.merge(@options))
  elb.load_balancers.each do |lb|
    env.ui.info "Id #{lb.id} - DNS #{lb.dns_name} - Zones: #{lb.availability_zones.join(',')} - Instances #{lb.instances.join(',')} "
  end
end

#status(selection = nil, 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
# File 'lib/mccloud/provider/aws/provider/status.rb', line 5

def status(selection=nil,options=nil)

  env.ui.info ""
  env.ui.info "Server(s) - provider #{@name}"

  printf "%-10s %-12s %-20s %-15s %-8s\n", "Name", "Instance Id", "IP", "Type","Status"
  80.times { |i| printf "=" } ; env.ui.info ""

  # List servers
  raw.servers.each do |vm|
    name="<no name set>"
    if !vm.tags["Name"].nil?
      name=vm.tags["Name"].strip
    end #end if

    if name.start_with?(self.filter)
      unless self.filter==""
        name[self.filter]=""
        printf "%-10s %-12s %-20s %-20s %-15s %-8s\n",name,vm.id, vm.public_ip_address, vm.private_ip_address,vm.flavor_id,vm.state
      else
        env.ui.info "Name: #{name}"
        env.ui.info "Instance Id: #{vm.id}"
        env.ui.info "Public Ip: #{vm.public_ip_address}"
        env.ui.info "Flavor: #{vm.flavor.name}"
        env.ui.info "State: #{vm.state}"
        80.times { |i| printf "=" } ; env.ui.info ""
      end
    end
  end #End 1 provider

  # List images
  env.ui.info ""
  env.ui.info "Image(s) - provider #{@name}"
  80.times { |i| printf "=" } ; env.ui.info ""
  images_list=raw.images.all({"Owner" => "self"})
  images_list.each do |image|
    printf "%-10s %-10s %-10s %-20s\n",image.id,image.name.gsub(/"#{filter}"/,''),image.state, image.description.to_s[0..20]
  end

  # List volumes
  env.ui.info ""
  env.ui.info "Volume(s) - provider #{@name}"
  80.times { |i| printf "=" } ; env.ui.info ""
  volume_list=raw.volumes.all()
  volume_list.each do |volume|
    printf "%-10s %-10s %-10s %-20s\n",volume.id,volume.device,volume.server_id, volume.size
  end
  env.ui.info ""

end