Class: Cf::Production

Inherits:
Thor
  • Object
show all
Includes:
Config
Defined in:
lib/cf/cli/production.rb

Overview

:nodoc: all

Instance Method Summary collapse

Methods included from Config

#config_file, #find_home, #get_api_key, #load_config, #save_config, #set_api_key, #set_target_uri

Instance Method Details

#add_unitsObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/cf/cli/production.rb', line 199

def add_units
  set_target_uri(false)
  set_api_key
  CF. = CF::Account.info.name
  run_title = options[:run_title].parameterize
  input_data = options[:input_data].presence
  
  if input_data =~ /^\// #checking absolute input data path
    input_data_file = input_data
  else
    unless File.exist?(input_data)
      say("The input data file named #{input_data} doesn't exist", :red) and return
    end
    input_data_file = "#{Dir.pwd}/#{input_data}"
  end
  units = CF::Run.add_units({:run_title => run_title, :file => input_data_file})
  if units['error'].present?
    say("Error: #{units['error']['message']}", :red) and exit(1)
  end

  # if result.status == "resumed"
  say("\"#{units['successfull']}\"!", :green)
  # end
end

#listObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cf/cli/production.rb', line 115

def list
  set_target_uri(false)
  set_api_key
  CF. = CF::Account.info.name
  param = {}
  current_page = 1

  if options['line'].present?
    line_title = options['line'].parameterize
    param.merge!({:line_title => line_title})

    if options['all']
      param.merge!({:page => "all"})
      current_page = 1
    end
    
    if page = options['page'].presence
      param.merge!({:page => page})
      current_page = page
    end

  else
    if options['all']
      param = {:page => "all"}
      current_page = 1
    end

    if page = options['page'].presence
      param.merge!({:page => page})
      current_page = page
    end
  end

  resp_runs = CF::Run.all(param)
  
  if resp_runs.has_key?('error')
    say("#{resp_runs['error']}", :red) and exit(1)
  end
  
  if resp_runs.has_key?("runs") && resp_runs['runs'].blank?
    say("\nRun list is empty.\n", :yellow) and return
  end
  
  if resp_runs['total_pages']
    say("\nShowing page #{current_page} of #{resp_runs['total_pages']} (Total runs: #{resp_runs['total_runs']})")
  end
  runs = resp_runs['runs'].presence
  runs.sort! {|a, b| a['title'] <=> b['title'] }
  runs_table = table do |t|
    t.headings = ["Run Title", 'URL']
    runs.each do |run|
      run = Hashie::Mash.new(run)
      t << [run.title, "http://#{CF.}.cloudfactory.com/runs/#{CF.}/#{run.title}"]
    end
  end
  say("\n")
  if runs_table.rows.present?
    say(runs_table)
  else
    say("No production run for line #{line_title}", :yellow)
  end
end

#resumeObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cf/cli/production.rb', line 180

def resume
  set_target_uri(false)
  set_api_key
  CF. = CF::Account.info.name
  result = CF::Run.resume(options['run_title'].parameterize)

  if result.error.present?
    say("Error: #{result.error.message}", :red) and exit(1)
  end

  # if result.status == "resumed"
  say("Run with title \"#{result.title}\" is resumed!", :green)
  # end
end

#start(title = nil) ⇒ 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cf/cli/production.rb', line 17

def start(title=nil)      
  line_destination  = Dir.pwd
  yaml_source       = "#{line_destination}/line.yml"

  set_target_uri(options[:live])
  set_api_key
  CF. = CF::Account.info.name
  
  if File.exist?("#{yaml_source}")
    line_yaml_dump = YAML::load(File.read(yaml_source).strip)
    line_title = line_yaml_dump['title'].parameterize        
    line = CF::Line.find(line_title)
    line = Hashie::Mash.new(line)
    say("#{line.error.message}", :red) and return if line.error.present?
  elsif options[:line].present?
    line = CF::Line.find(options[:line])
    line = Hashie::Mash.new(line)
    if line.error.blank?
      line_title = options[:line]
    else
      say("#{line.error.message}", :red) and return
    end
  else
    say("Looks like you're not in the Line directory or did not provide the line title to use the line", :red) and return
  end

  if title.nil?
    if line_title =~  /\w\/\w/
      run_title       = "#{line_title.split("/").last}-#{Time.new.strftime('%y%b%e-%H%M%S')}".downcase
    else
      run_title       = "#{line_title}-#{Time.new.strftime('%y%b%e-%H%M%S')}".downcase
    end
  else
    run_title       = "#{title.parameterize}-#{Time.new.strftime('%y%b%e-%H%M%S')}".downcase
  end

  input_data = options[:input_data].presence
  
  if input_data =~ /^\// #checking absolute input data path
    input_data_file = input_data
  else
    if Dir.exist?("#{line_destination}/input")
      input_data_dir = "#{line_destination}/input"
      input_files = Dir["#{input_data_dir}/*.csv"]
      file_count = input_files.size
      case file_count
      when 0
        say("No input data file present inside the input folder", :red) and return
      when 1
        input_data_file = "#{Dir.pwd}/input/#{extract_name(input_files.first)}"
      else
        # Let the user choose the file
        chosen_file = nil
        choose do |menu|
          menu.header = "Input data files"
          menu.prompt = "Please choose which file to be used as input data: "

          input_files.each do |item|
            menu.choice(extract_name(item)) do
              chosen_file = extract_name(item)
              say("Using the file #{chosen_file} as input data")
            end
          end
        end
        input_data_file = "#{Dir.pwd}/input/#{chosen_file}"
      end
    else
      unless File.exist?(input_data)
        say("The input data file named #{input_data} doesn't exist", :red) and return
      end
      input_data_file = "#{Dir.pwd}/#{input_data}"
    end
  end
  
  unless File.exist?(input_data_file)
    say("The input data file named #{input_data} is missing", :red) and return
  end
  
  say "Creating a production run with title #{run_title}", :green
  run = CF::Run.create(line_title, run_title, input_data_file)
  if run.errors.blank?
    display_success_run(run)
  else
    say("Error: #{run.errors}", :red)
  end
end