Module: Judo::CLIHelpers

Defined in:
lib/judo/cli_helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_group_dirObject



3
4
5
# File 'lib/judo/cli_helpers.rb', line 3

def default_group_dir
  File.expand_path(File.dirname(__FILE__) + "/../../default")
end

#do_commit(judo, args) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/judo/cli_helpers.rb', line 46

def do_commit(judo, args)
			name = args.first
			if name =~ /:(.+)$/
    group = judo.get_group($1)
  else
    raise JudoError, "Invalid group name '#{name}'"
  end
			group.commit
end

#do_generate(judo, args) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/judo/cli_helpers.rb', line 7

def do_generate(judo, args)
			name = args.first
			if File.exists?(name)
puts "group already exists at /#{name}"
			else
 FileUtils.cp_r(default_group_dir, name)
			end
end

#do_groups(judo) ⇒ Object



67
68
69
70
71
72
# File 'lib/judo/cli_helpers.rb', line 67

def do_groups(judo)
  printf "  SERVER GROUPS\n"
  judo.groups.each do |g|
    printf "%-18s %s servers\n", g.name, g.servers.size
  end
end

#do_info(judo, server) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/judo/cli_helpers.rb', line 129

def do_info(judo, server)
  puts "[ #{server} ]"
  printf "    %-24s: %s\n", "ID", server.id
  printf "    %-24s: %s\n", "Group", server.group.name
  printf "    %-24s: %s\n", "Animated From", server.clone if server.clone
  printf "    %-24s: %s\n", "Elastic Ip", server.elastic_ip if server.elastic_ip
  sub_info("EC2", server.ec2_instance) do |i|
    [:aws_instance_id, :ssh_key_name, :aws_availability_zone, :aws_state, :aws_image_id, :dns_name, :aws_instance_type, :private_dns_name, :aws_launch_time, :aws_groups ].each do |k|
      printf "    %-24s: %s\n",k, i[k]
    end
  end
  sub_info("METADATA", (server. || {}).keys) do |key|
    printf("    %-24s: %s\n", key, server.[key] )
  end
  sub_info("VOLUMES", server.ec2_volumes) do |v|
    printf "    %-13s %-10s %-10s %4d  %-10s %-8s\n",
    v[:aws_id],
    v[:aws_status],
    v[:zone],
    v[:aws_size],
    v[:aws_attachment_status],
    v[:aws_device]
  end
  sub_info("SNAPSHOTS", server.snapshots) do |s|
    printf "    %-10s %-15s %-8s %-5s\n", s.name, s.group_name, s.version_desc, "#{s.ec2_ids.size}v"
  end
end

#do_ips(judo) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/judo/cli_helpers.rb', line 88

def do_ips(judo)
  ips = judo.ips.sort { |a,b| a[:assigned_to].to_s <=> b[:assigned_to].to_s }

  format = "%15s %20s %20s\n"
  printf format, "IP", "ATTACHED TO", "CONFIGURED FOR"
  printf "%s\n", ("-"*57)

  ips.each do |ip|
    attached = ip[:attached_to] ? ip[:attached_to].name : ip[:instance_id]
    assigned = ip[:assigned_to] ? ip[:assigned_to].name : ""
    printf format, ip[:ip], attached, assigned
  end
end

#do_list(judo, args) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/judo/cli_helpers.rb', line 110

def do_list(judo, args)
  printf "  SERVERS\n"
  printf "%s\n", ("-" * 80)
  args << ":all" if args.empty?
  each_server(judo,args) do |s|
    printf "%-32s %-12s %-7s %-11s %-11s %-10s %-3s %s\n", s.name, s.group.name, s.version_desc, s.get("instance_id"), s.size_desc, s.ec2_state, "ebs:#{s.volumes.keys.size}", s.has_ip? ? "ip" : ""
  end
end

#do_snapshots(judo, args) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/judo/cli_helpers.rb', line 102

def do_snapshots(judo, args)
  printf "  SNAPSHOTS\n"
  printf "%s\n", ("-" * 80)
  judo.snapshots.each do |snapshot|
    printf "%-15s %-25s %-15s %-10s %s\n", snapshot.name, snapshot.server_name, snapshot.group_name, snapshot.version_desc, "ebs:#{snapshot.ec2_ids.size}"
  end
end

#do_volumes(judo) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/judo/cli_helpers.rb', line 74

def do_volumes(judo)
  vols = judo.volumes.sort { |a,b| [ a[:assigned_to].to_s, a[:instance_id].to_s ] <=> [ b[:assigned_to].to_s, b[:instance_id].to_s ] }

  format = "%13s %6s %12s  %-10s %-16s %-16s\n"
  printf format, "AWS_ID", "SIZE", "AWS_STATUS", "AWS_DEVICE", "ATTACHED TO", "CONFIGURED FOR"
  printf "%s\n", ("-" * 80)

  vols.each do |v|
    attached = v[:attached_to] ? v[:attached_to].name : v[:instance_id]
    assigned = v[:assigned_to] ? v[:assigned_to].name : ""
    printf format, v[:id], v[:size], v[:status], v[:device], attached, assigned
  end
end

#each_server(judo, args, &blk) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/judo/cli_helpers.rb', line 16

def each_server(judo, args, &blk)
  raise JudoError, "No servers specified - use :all for all servers" if args.empty?
  servers = judo.find_servers_by_name_or_groups_with_not(args)
  servers.each do |server|
    begin
      blk.call(server)
    rescue JudoInvalid => e
      puts "#{server} - #{e.message}"
    end
  end
end

#mk_server_names(judo, args, &blk) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/judo/cli_helpers.rb', line 28

def mk_server_names(judo, args, &blk)
  args.each do |arg|
    name,group = arg.split(":")
    raise JudoError, "You must specify a group on create and launch" unless group
    names = if name =~ /^[12345]$/
      (1..(name.to_i)).each do
        blk.call(judo.mk_server_name(group), group)
      end
    elsif name == ""
      blk.call(judo.mk_server_name(group), group)
    elsif name =~ /^\d+$/
      raise JudoError, "You can batch-create between 1 and 5 servers" if count < 1 or count > 5
    else
      blk.call(name, group)
    end
  end
end

#mk_servers(judo, options, args, start) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/judo/cli_helpers.rb', line 56

def mk_servers(judo, options, args, start)
  mk_server_names(judo, args) do |name, group|
    begin
      server = judo.create_server(name, group, options)
      server.start(options) if start
    rescue JudoInvalid => e
      puts "#{server} - #{e.message}"
    end
  end
end

#sub_info(header, data, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/judo/cli_helpers.rb', line 119

def sub_info(header, data, &block)
  return if data == []
  return if data == {}
  return if data.nil?
  puts "  [ #{header} ]"
  [ data ].flatten.each do |d|
    block.call(d)
  end
end