Class: Sensible::Sensible

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sensibleFileName, opts, args) ⇒ Sensible

Returns a new instance of Sensible.



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
# File 'lib/sensible.rb', line 18

def initialize(sensibleFileName, opts, args)
  @opts = opts
  @args = args


  file_name = opts.file || sensibleFileName
  unless File.exist?(file_name)
      Logger.error("Required file not found: #{file_name}")
      exit(1)
  end

  # Load the Sensile file
  sensible_file_data = YAML.load_file(file_name)


  # Recursively go through all the tasks inside the sensible file
  # Add them to a list, that we will go through later
  @tasks = []
  def process_task(task_line, task_user = nil)
    task = nil
    
    # Handle inline tasks inside root sensible file
    if task_line.class == Hash
      task = Task.new(task_line, task_line, task_user, self)
    end

    if task_line.is_a?(String)
      task_yaml = YAML.load_file(task_line + '.yml')
      task = Task.new(task_yaml, task_line, task_user, self)
    end

    if task.require != nil
      task.require.each do |path|
        process_task(path, task.user)
      end
    end

    @tasks << task
  end 

  sensible_file_data.each do |key, value|
    case key
      when "require"
        value.each do |path|
          process_task(path)
        end
      when "tasks"
        value.each do |path|
          process_task(path)
        end
    end 
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



13
14
15
# File 'lib/sensible.rb', line 13

def args
  @args
end

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/sensible.rb', line 12

def opts
  @opts
end

#tasksObject (readonly)

Returns the value of attribute tasks.



15
16
17
# File 'lib/sensible.rb', line 15

def tasks
  @tasks
end

Class Method Details

.init(opts) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sensible.rb', line 193

def self.init(opts)
  sensible_file_name = "sensible.yml"

  if opts.file
    if opts.file.end_with?(".yml")
      sensible_file_name = opts.file
    else
      sensible_file_name = opts.file + ".yml"
    end
  end

  if not File.exist?(sensible_file_name)
    File.open(sensible_file_name, "w") do |f| 
      f.write("        ---\n        packages:\n        requirements:\n      EOF\n    end\n    Logger.success(\"Created \#{sensible_file_name}!\")\n  else \n    Logger.error(\"Cannot create \#{sensible_file_name}, it already exists!\")\n  end\nend\n")

Instance Method Details

#checkObject

Run all the checks for packages and requirements



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
# File 'lib/sensible.rb', line 74

def check
  # Prewarm sudo, to prevent asking too much
  shell = Shell.new(self)
  shell.run_command('sudo -v', show_output: true)
  
  @tasks.each_with_index do |task, index|
    user = nil
    if task.user
      user = "(User: #{task.user})"
    end

    pastel = Pastel.new
    if index > 0
      Logger.log("\n#{pastel.bold(task.name)} #{user}")
    else
      Logger.log("#{pastel.bold(task.name)} #{user}")
    end

    # Do an environment test
    # if @opts.env
    #   # If requirement env is not define, we expect it should always be installed regardless of environment
    #   # If user has defined an environment, skip if the set environment isn't in the package enviroment list 
    #   next if requirement.env.any? && !requirement.env.include?(@opts.env)
    # end
    if task.packages.length > 0
      if task.do_packages_check
        Logger.success("Task packages installed!")
      else
        Logger.danger("Task packages NOT installed!")
      end
    end
    
    # Only do check if script exists
    if task.script 
      if task.do_check
        Logger.success("Task is installed!")
      else
        Logger.danger("Task is NOT installed!")
      end
    end
  end
end

#installObject



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
# File 'lib/sensible.rb', line 117

def install
  # Prewarm sudo, to prevent asking too much
  shell = Shell.new(self)
  shell.run_command('sudo -v', show_output: true)

  
  @tasks.each_with_index do |task, index|
    user = nil
    if task.user
      user = "(#{task.user})"
    end

    pastel = Pastel.new
    if index > 0
      Logger.log("\n#{pastel.bold(task.name)} #{user}")
    else
      Logger.log("#{pastel.bold(task.name)} #{user}")
    end


    # Do an environment test
    # if @opts.env
    #   # If requirement env is not define, we expect it should always be installed regardless of environment
    #   # If user has defined an environment, skip if the set environment isn't in the package enviroment list
    #   next if requirement.env.any? && !requirement.env.include?(@opts.env)
    # end
    if task.packages.length > 0
      if task.do_packages_check
        Logger.success("Task packages installed!")
      else
        Logger.info("Installing packages...\r", use_print: true)
        if task.do_packages_install
          Logger.clear_line
          Logger.success("Task packages installed!")
        else
          Logger.clear_line
          Logger.danger("Task packages NOT installed!")
        end

        Logger.flush
      end
    end

    if task.do_check
      Logger.success("Task is installed!")
    else
      # Handle copy task
      if task.copy
        Logger.info("Copying files...\r", use_print: true)
        if task.do_copy
          Logger.clear_line
          Logger.success("Files was copied!")
        else
          Logger.clear_line
          Logger.danger("Files was NOT copied!")
        end
        Logger.flush
      end

      # Handle task script
      if task.script
        Logger.info("Running task...\r", use_print: true)
        if task.do_script
          Logger.clear_line
          Logger.success("Task is installed!")
        else
          Logger.clear_line
          Logger.danger("Task is NOT installed!")
        end
        Logger.flush
      end
    end
  end

end

#process_task(task_line, task_user = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sensible.rb', line 36

def process_task(task_line, task_user = nil)
  task = nil
  
  # Handle inline tasks inside root sensible file
  if task_line.class == Hash
    task = Task.new(task_line, task_line, task_user, self)
  end

  if task_line.is_a?(String)
    task_yaml = YAML.load_file(task_line + '.yml')
    task = Task.new(task_yaml, task_line, task_user, self)
  end

  if task.require != nil
    task.require.each do |path|
      process_task(path, task.user)
    end
  end

  @tasks << task
end

#task_create(task_name) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/sensible.rb', line 264

def task_create(task_name)
  tasks_path = "#{@sensible_folder}/#{@tasks_folder}"
  task_file_path = "#{tasks_path}/#{task_name}.yml"
  
  # Make sure the task don't already exist
  if File.exist?(task_file_path)
    pastel = Pastel.new
    Logger.error("Task: #{pastel.bold(task_name)} already exist!")
    exit(1)
  end
  
  # Create the task yaml file
  File.open(task_file_path, "w") do |f| 
    f.write("      ---\n      name:\n      description:\n      install:\n    EOF\n  end\n\n  pastel = Pastel.new\n  Logger.success(\"Created the task: \#{pastel.bold(\"\#{task_name}.yml\")}\")  \nend\n")

#task_listObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/sensible.rb', line 246

def task_list
  # Parse tasks
  tasks_path = "#{@sensible_folder}/#{@tasks_folder}"
  task_files = Dir.children(tasks_path)

  tasks = []
  for task_file in task_files
    tasks << Task.new(YAML.load_file("#{tasks_path}/#{task_file}"), task_file, self)
  end

  puts "Here is available tasks inside #{@sensible_folder}/#{tasks_folder}" if @opts.verbose

  pastel = Pastel.new
  for task in tasks
    Logger.log("#{pastel.blue.bold(task.file_name)}: #{task.name}")
  end
end

#task_run(task_name) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/sensible.rb', line 218

def task_run(task_name)
  # Load and parse the task file
  task_file_path = "#{@sensible_folder}/#{@tasks_folder}/#{task_name}.yml"
  
  # If task is not found, exit!
  if not File.exist?(task_file_path)
    pastel = Pastel.new
    Logger.error("Task: #{pastel.bold(task_name)} does not exist!")
    exit(1)
  end

  task = Task.new(YAML.load_file(task_file_path), "#{task_name}.yml", self)

  pastel = Pastel.new
  Logger.info("Running task: #{pastel.bold(task_name)}")
  
  # Check if we need to rerun the task
  if !task.do_check
    if !task.do_install
      Logger.error("The tasked failed!")
    else
      Logger.success("The task ran succesfully!")
    end
  else
    puts "Task check is already met"
  end
end