Class: NliPipeline::SetupPipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/nli_pipeline/setup_pipeline.rb

Overview

Class used to parse arguments from bin/setup_pipeline executable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSetupPipeline

Set up class attributes



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
# File 'lib/nli_pipeline/setup_pipeline.rb', line 14

def initialize
  @version = NliPipeline::VERSION

  @commands = {
    undo:              proc { |fm| fm.load_from_backup },
    show_last_created: proc { |fm| puts(fm.last_created_files.to_s) }
  }

  @docker_commands = {
    docker_build:          proc { |dm| dm.build },
    docker_build_commit:   proc { |dm| dm.build_commit? },
    docker_build_and_save: proc { |dm| dm.build_and_save },
    docker_deploy_branch:  proc { |dm| dm.deploy_branch },
    docker_deploy_master:  proc { |dm| dm.deploy_master },
    docker_exec:           proc { |dm| dm.docker_exec },
    docker_run:            proc { |dm| dm.run },
    docker_save:           proc { |dm| dm.save }
  }

  @dir_error = "
    The first argument to setup_pipeline must be a valid directory.
    For example, setting up a pipeline in the current directory:

      setup_pipeline $PWD

    The exceptions to this are:\n
      setup_pipeline --version
      setup_pipeline --help
      setup_pipeline --show-commands
      setup_pipeline --show-flags
  "
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



11
12
13
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11

def commands
  @commands
end

#dir_errorObject (readonly)

Returns the value of attribute dir_error.



11
12
13
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11

def dir_error
  @dir_error
end

#docker_commandsObject (readonly)

Returns the value of attribute docker_commands.



11
12
13
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11

def docker_commands
  @docker_commands
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11

def options
  @options
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/nli_pipeline/setup_pipeline.rb', line 11

def version
  @version
end

Instance Method Details

#delegate_to_managers(flags, command) ⇒ Object

handle what manager object to create based on flags / command

Parameters:

  • flags (Hash[Symbol: String])
  • command (String)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/nli_pipeline/setup_pipeline.rb', line 176

def delegate_to_managers(flags, command)
  if @docker_commands.key?(command)
    # always fail on error if anything goes wrong with a deploy
    flags[:fail_on_error] = true if command.to_s.include?('deploy')
    dm = NliPipeline::DockerManager.new(**flags)
    @docker_commands[command].call(dm)
  # if not DockerManager, use FileManager
  else
    fm = NliPipeline::FileManager.new(**flags)
    if @commands.key?(command)
      @commands[command].call(fm)
    # is no command is specified, copy example files
    else
      fm.copy_example_files
    end
  end
end

#flags_or_help_screenObject

handle case where invalid flag is passed show user help screen



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nli_pipeline/setup_pipeline.rb', line 158

def flags_or_help_screen
  begin
    flags = parse_flags
  rescue OptionParser::InvalidOption => e
    puts(e.to_s.red)
    # inject --help as first arg
    ARGV.insert(0, '--show-flags')
    #  call flags again to display help dialogue
    parse_flags
    # re-throw error
    raise e
  end
  flags
end

#help(opts) ⇒ Object

print help screen to console

Parameters:

  • opts (OptionParser)


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

def help(opts)
  how_to_use = "HOW TO USE:\n
    setup_pipeline $PWD

    #{opts}

    FileManager commands:   #{@commands.keys}

    DockerManager commands: #{@docker_commands.keys}

  "

  errors = "COMMON ERRORS:\n
    #{@dir_error}
  "

  docs = "FOR MORE INFORMATION PLEASE READ THE DOCS:\n
    on bitbucket:\t\thttps://bitbucket.org/nlireland/nli-pipeline-gem\n
    or on ruby gems:\t\thttps://rubygems.org/gems/nli_pipeline\n
  "

  puts("#{how_to_use}\n#{errors}\n#{docs}")
end

#mainObject

Method called by bin/setup_pipeline Parse commands (command line arguments) and pass flags / flags to parse_flags



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/nli_pipeline/setup_pipeline.rb', line 200

def main
  # byebug
  flags = flags_or_help_screen
  # flags will be false if a flag that exits early is passed e.g. false
  # otherwise flags will be some kind of hash, all of which are truthy, even {}
  return false unless flags

  # TODO
  # document this better or
  # get rid of commands? use separate executables in bin for FileManager vs DockerManager?

  # command is the second argument
  # e.g. setup_pipeline $PWD COMMAND_GOES_HERE --flags --go --here
  args = parse_args
  flags[:path] = args[:path]
  command = args[:command]

  delegate_to_managers(flags, command)
end

#parse_argsObject

Commandline Arguments:

$1 (file path) (required)
$2 (command to run)

Raises:

  • (ArgumentError)

    if $1 is missing or is not a valid directory



147
148
149
150
151
152
153
154
# File 'lib/nli_pipeline/setup_pipeline.rb', line 147

def parse_args
  raise ArgumentError, "\n#{@dir_error}\n" unless ARGV[0] && Dir.exist?(ARGV[0])

  args = {}
  args[:path] = ARGV[0]
  args[:command] = ARGV[1].to_sym unless ARGV[1].nil? || ARGV[1].empty?
  args
end

#parse_flagsObject

Commandline Flags:

$1 (file path) (required)
--version
--extension=(string: file extension)
--debug (show all system commands when enabled)


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
# File 'lib/nli_pipeline/setup_pipeline.rb', line 86

def parse_flags
  options = {}
  OptionParser.new do |opts|
    @options = opts
    # break out of function, don't throw error about passing directory path
    opts.on('-h', '--help') do |_v|
      help(opts)
      return false
    end
    opts.on('--show-commands') do |_v|
      show_commands
      return false
    end
    opts.on('--show-flags') do |_v|
      puts(self.options)
      return false
    end
    opts.on('-v', '--version') do |_v|
      puts(self.version)
      return false
    end
    opts.on('-cmds=', '--commands=a,b,c', Array, 'Array of bash commands') do |v|
      options[:bash_commands] = v
    end
    opts.on('--debug') do |v|
      options[:debug] = v
    end
    opts.on("--extension=[[\w\.]+]", String, 'String file extension') do |v|
      options[:extension] = v
    end
    # override git branch (for docker deploy)
    opts.on("--git-branch=[[\w\.]+]", String, 'String git branch') do |v|
      options[:git_branch] = v
    end
    opts.on('--fail-on-error') do |v|
      options[:fail_on_error] = v
    end
    opts.on("--image=[([\w\-\.\:]+)]", String, 'String docker image') do |v|
      options[:image_name] = v
    end
    opts.on("--mount=[([\w\-\.\:]+)]", String, 'String directory to mount') do |v|
      options[:mount] = v
    end
    opts.on('--ports=a,b,c', Array, 'Array of ports') do |v|
      options[:ports] = v
    end
    opts.on("--proxy=[([\w\-\.\:]+)]", String, 'String proxy') do |v|
      options[:proxy] = v
    end
    opts.on("--upstream-image=[([\w\-\.\:]+)]", String, 'String dockerhub image') do |v|
      options[:upstream_image_name] = v
    end
  end.parse!
  options
end

#show_commandsObject

simple formatter



48
49
50
51
52
53
# File 'lib/nli_pipeline/setup_pipeline.rb', line 48

def show_commands
  puts('FileManager commands')
  puts(@commands.keys)
  puts('DockerManager commands')
  puts(@docker_commands.keys)
end