Class: Sensible::Task

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taskHash, file_name, user = nil, sensible) ⇒ Task

Returns a new instance of Task.



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

def initialize(taskHash, file_name, user = nil, sensible)
  @name = taskHash['name']
  @check = taskHash['check']
  @script = taskHash['script']
  @require = taskHash['require']
  @env = taskHash['env'] || []
  @file_name = file_name
  @description = taskHash['description']
  @show_output = taskHash['showOutput']
  @copy = taskHash['copy']
  
  # If task is initalized with user, force use of that
  if user != nil
    @user = user
  end
  
  # Always use the user from the task
  if taskHash['user'] != nil
    @user = taskHash['user']
  end

  @sensible = sensible
  
  @packages = []
  if taskHash['packages']
    taskHash['packages'].each do |distro, packages|
      packages.each do |packageName|
        @packages << Package.new(packageName, distro, sensible)
      end
    end
  end
end

Instance Attribute Details

#checkObject (readonly)

Returns the value of attribute check.



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

def check
  @check
end

#copyObject (readonly)

Returns the value of attribute copy.



19
20
21
# File 'lib/sensible/task.rb', line 19

def copy
  @copy
end

#descriptionObject (readonly)

Returns the value of attribute description.



17
18
19
# File 'lib/sensible/task.rb', line 17

def description
  @description
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



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

def file_name
  @file_name
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/sensible/task.rb', line 9

def name
  @name
end

#packagesObject (readonly)

Returns the value of attribute packages.



10
11
12
# File 'lib/sensible/task.rb', line 10

def packages
  @packages
end

#requireObject (readonly)

Returns the value of attribute require.



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

def require
  @require
end

#scriptObject (readonly)

Returns the value of attribute script.



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

def script
  @script
end

#sensibleObject (readonly)

Returns the value of attribute sensible.



8
9
10
# File 'lib/sensible/task.rb', line 8

def sensible
  @sensible
end

#show_outputObject

Returns the value of attribute show_output.



18
19
20
# File 'lib/sensible/task.rb', line 18

def show_output
  @show_output
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#check_exists(path) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sensible/task.rb', line 140

def check_exists(path)
  is_folder = path.end_with?('/')
  expanded_path = File.expand_path(path)

  if is_folder == true
    return Dir.exist?(expanded_path)
  else
    return File.exist?(expanded_path)
  end

  # Handle remote
end

#do_checkObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sensible/task.rb', line 82

def do_check
  # Always return false to force copy
  if @copy != nil
    return false
  end

  # If check is not set, always run the task
  if @check == nil
    return false
  end

  # If there is no check, default to false, to force task to script every time
  shell = Shell.new(@sensible)
  return shell.run_command(@check, @user, show_output: @show_output)
end

#do_copyObject



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

def do_copy 
  origin = File.expand_path(@copy['origin'])
    destination = @copy['dest']
    destination_expanded = File.expand_path(@copy['dest'])
    
    puts destination

    commmand = nil
    
    if @sensible.opts.host 
      if @user 

        command = "rsync -avu --delete #{Shellwords.escape(origin)} root@#{@sensible.opts.host}:#{Shellwords.escape(destination.sub('~', "/home/#{user}"))}"
      else 
        command = "rsync -avu --delete #{Shellwords.escape(origin)} root@#{@sensible.opts.host}:#{Shellwords.escape(destination)}"
      end
      
    else 
      command = "rsync -avu --delete #{Shellwords.escape(origin)} #{Shellwords.escape(destination_expanded)}"
    end
    
    if @sensible.opts.verbose 
      system(command)
    else 
      system(command, out: File::NULL, err: File::NULL)
    end
    
    return $?.success?
end

#do_packages_checkObject

Check if the packages in this task is installed



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sensible/task.rb', line 55

def do_packages_check
  has_all_packages_install = true

  @packages.each do |package|
    package.name

    if !package.do_check
      has_all_packages_install = false
    end
  end

  return has_all_packages_install     
end

#do_packages_installObject

Install the packages in this task



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sensible/task.rb', line 70

def do_packages_install
  has_all_packages_install = true

  @packages.each do |package|
    if !package.do_install
      has_all_packages_install = false
    end
  end

  return has_all_packages_install
end

#do_scriptObject



129
130
131
132
133
# File 'lib/sensible/task.rb', line 129

def do_script
  # TODO: Handle the show output property!
  shell = Shell.new(@sensible)
  return shell.run_command(@script, @user, show_output: @show_output)         
end

#do_verifyObject



135
136
137
# File 'lib/sensible/task.rb', line 135

def do_verify
  return true
end