Method: Cf::Production#start

Defined in:
lib/cf/cli/production.rb

#start(title = nil) ⇒ Object



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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cf/cli/production.rb', line 72

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']
  available_balance = CF::Account.info['balance']['available_balance']
  if available_balance.to_f <= 0

    say("****************************************************************************************", :red)
    say("***  Warning! Your account balance is 0. Please recharge your account balance ASAP.  ***", :red)
    say("****************************************************************************************", :red)
  elsif available_balance.to_f <= 500
    say("***********************************************************************************************", :yellow)
    say("*** Warning! Your account balance is #{available_balance.to_s} cf cents. Please recharge your account balance.  ***", :yellow)
    say("***********************************************************************************************", :yellow)
  end


  if options[:line].present?
    line = CF::Line.find(options[:line])
    if line.nil?
      say("Line named #{options[:line]} does not exist !!!", :red) and exit(1)
    else
      line = Hashie::Mash.new(line)
      line_title = options[:line]
    end
  elsif 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)
    if line.nil?
      say("Line named #{options[:line]} does not exist !!!", :red) and exit(1)
    else
      line = Hashie::Mash.new(line)
    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%d-%H%M%S')}".downcase
    else
      run_title       = "#{line_title}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
    end
  else
    run_title       = "#{title.parameterize}-#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
  end

  say("Title is longer than expected. The system accepts only 40 no of characters", :red) and exit(1) if title.length > 40

  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

      #### zipping csv file
      say("CSV file size is greater than 2 MB. Aborting production start.", :red) and return if (File.size(input_data_file).to_f/(1024*1000) > 2)
      say("Zipping csv file...", :green)
      zipfile_name = "input/input.zip"
      Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
        begin
          zipfile.add("input.csv", input_data_file)
        rescue
          zipfile.remove("input.csv")
          zipfile.add("input.csv", input_data_file)
        end
      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

  gold_standards = options[:gold_standards]
  say "Creating a production run with title #{run_title}", :green
  run = CF::Run.create(line_title, run_title, zipfile_name, gold_standards)
  if run.errors.blank?
    display_success_run(run)
    ## remove the input/input.zip file so that next time when production run is started it wont conflict
    if File.exist?(zipfile_name) then
        FileUtils.rm_rf zipfile_name
      end
  else
    say("Error: #{run.errors}", :red)
  end
end