Module: Capricorn::Helpers

Constant Summary collapse

DEFAULT_CONFIG =
<<-EOH
cluster_name:
  host: localhost
  port: 3457
  username: dummy
  password: dummy
EOH

Instance Method Summary collapse

Instance Method Details

#applicationObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/capricorn-client/helpers.rb', line 56

def application
  @application ||= begin
    application = nil
    if options.application
      application = options.application
    elsif environment['application']
      application = environment['application']
    elsif application_ids.size == 1
      application = application_ids.first
      info "Selected default application: #{application}"
    else
      halt "Please select a application!"
    end

    if application
      application = application_ids.abbrev[application]
    end

    unless application_ids.include?(application)
      halt "I don't know this application!"
    end

    [machine, application]
  end
end

#application_idsObject



13
14
15
16
17
18
19
20
21
# File 'lib/capricorn-client/helpers.rb', line 13

def application_ids
  @application_ids ||= begin
    apps = applications.collect do |app|
      app[1]
    end
    apps.flatten!
    apps
  end
end

#application_infoObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/capricorn-client/helpers.rb', line 41

def application_info
  @application_info ||= begin
    apps = applications
    app = apps.detect do |app|
      app[1] == application.last
    end

    unless app
      halt 'Application not found!'
    end

    app
  end
end

#applicationsObject



23
24
25
26
27
# File 'lib/capricorn-client/helpers.rb', line 23

def applications
  @applications ||= begin
    client.call.applications.all(machine.to_sym).last || []
  end
end

#clientObject



211
212
213
214
215
# File 'lib/capricorn-client/helpers.rb', line 211

def client
  host    = (cluster['host'] || 'localhost').to_s
  port    = (cluster['port'] || 3457).to_i
  @client ||= BERTRPC::Service.new(host, port)
end

#clusterObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/capricorn-client/helpers.rb', line 134

def cluster
  @cluster ||= begin
    cluster = nil
    if options.cluster
      cluster = options.cluster
    elsif environment['cluster']
      cluster = environment['cluster']
    elsif config.size == 1
      cluster = config.keys.first
      info "Selected default cluster: #{cluster}"
    else
      halt "Please select a cluster!"
    end

    if cluster
      cluster = config.keys.abbrev[cluster]
    end

    unless config.keys.include?(cluster)
      halt "I don't know this cluster!"
    end

    config[cluster]
  end
end

#configObject



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/capricorn-client/helpers.rb', line 198

def config
  @config ||= begin
    config_path = File.expand_path('~/.capricorn/config.yml')
    unless File.file?(config_path)
      FileUtils.mkdir_p(File.dirname(config_path))
      File.open(config_path, 'w+', 0600) { |file| file.write DEFAULT_CONFIG }
      info "Please edit your config file in ~/.capricorn/config.yml"
      exit 2
    end
    YAML.load_file(config_path)
  end
end

#environmentObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/capricorn-client/helpers.rb', line 160

def environment
  @environment ||= begin
    environment = nil
    if options.environment
      environment = options.environment
    elsif local_config.size == 1
      environment = local_config.keys.first
      info "Selected default environment: #{environment}"
    elsif local_config.size > 1
      halt "Please select an environment!"
    end

    if environment
      environment = local_config.keys.abbrev[environment]
    end

    config = nil
    if environment.nil?
      config = {}
    elsif local_config.keys.include?(environment)
      config = local_config[environment]
    else
      halt "I don't know this environment!"
    end
    config
  end
end

#halt(msg, continue = false) ⇒ Object



221
222
223
224
# File 'lib/capricorn-client/helpers.rb', line 221

def halt(msg, continue=false)
  shell.say_status('Error', msg, :red)
  exit(1) unless continue
end

#info(msg) ⇒ Object



217
218
219
# File 'lib/capricorn-client/helpers.rb', line 217

def info(msg)
  shell.say_status('Info', msg)
end

#local_configObject



188
189
190
191
192
193
194
195
196
# File 'lib/capricorn-client/helpers.rb', line 188

def local_config
  @local_config ||= begin
    if File.file?('.capricorn.yml')
      YAML.load_file('.capricorn.yml')
    else
      {}
    end
  end
end

#machineObject



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
# File 'lib/capricorn-client/helpers.rb', line 82

def machine
  @machine ||= begin
    machine = nil
    if options.machine
      machine = options.machine
    elsif environment['machine']
      machine = environment['machine']
    elsif machines.size == 1
      machine = machines.first
      info "Selected default machine: #{machine}"
    else
      halt "Please select a machine!"
    end

    if machine
      machine = machines.abbrev[machine]
    end

    unless machines.include?(machine)
      halt "I don't know this machine!"
    end

    machine
  end
end

#machinesObject



29
30
31
32
33
# File 'lib/capricorn-client/helpers.rb', line 29

def machines
  @machines ||= (client.call.machines.all || []).collect do |machine|
    machine.to_s
  end
end

#nodeObject



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/capricorn-client/helpers.rb', line 108

def node
  @node ||= begin
    node = nil
    if options.machine
      node = options.machine
    elsif environment['machine']
      node = environment['machine']
    elsif nodes.size == 1
      node = nodes.first
      info "Selected default node: #{node}"
    else
      halt "Please select a node!"
    end

    if node
      node = nodes.abbrev[node]
    end

    unless nodes.include?(node)
      halt "I don't know this node!"
    end

    node
  end
end

#nodesObject



35
36
37
38
39
# File 'lib/capricorn-client/helpers.rb', line 35

def nodes
  @nodes ||= (client.call.runtime.nodes || []).collect do |node|
    node.to_s
  end
end