Class: EPM::Deploy

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

Instance Method Summary collapse

Constructor Details

#initialize(def_file, settings = {}) ⇒ Deploy

Returns a new instance of Deploy.



5
6
7
8
9
10
11
# File 'lib/epm/deploy.rb', line 5

def initialize def_file, settings={}
  @dirname  = File.dirname(File.absolute_path(def_file))
  @log_file = File.join(ENV['HOME'], '.epm', 'deployed-log.csv')
  setup settings
  @brain    = {}
  @def_file = def_file
end

Instance Method Details

#blob(command) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/epm/deploy.rb', line 143

def blob command
  file_name = command.shift
  file_data = File.read( File.join( @dirname, file_name ) )
  name = command.shift
  begin
    require 'c3d'
    C3D::SetupC3D.new
    C3D::ConnectTorrent.supervise_as :puller, {
        username: ENV['TORRENT_USER'],
        password: ENV['TORRENT_PASS'],
        url:      ENV['TORRENT_RPC'] }
    c = C3D::Blobber.new file_data
    blob_id = '0x' + c.btih + c.sha1_trun
  rescue
    p 'c3d is not installed, please install it to use the Blob command.'
    blob_id = '0x'
  end
  p "Blobbed #{file_name} with a name of #{name} and a blob_id of #{blob_id}"
  @brain[name] = blob_id
end

#deploy(command) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/epm/deploy.rb', line 51

def deploy command
  deploy_k = command.shift
  k_name = command.shift
  p "Deploying #{deploy_k} with name of #{k_name.gsub(/(\{|\})/,'')}."
  k_address = EPM::Create.new(File.join(@dirname, deploy_k), @settings).deploy
  @brain[k_name] = k_address
  p "#{k_name} => #{k_address}"
end

#deploy_packageObject



13
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
46
47
48
49
# File 'lib/epm/deploy.rb', line 13

def deploy_package
  get_remote_if_remote
  if @def_file
    p "Deploying: #{@def_file}."
    commands = File.readlines @def_file
    commands = commands.reject{|cmd| cmd[/\A#/] || cmd[/\A$/]}.map{|cmd| cmd.gsub("\n",'')}
    commands = commands.inject([]) do |arr,ele|
      ele[/\A\S+/] ? arr << [ele] : arr[-1] << ele.strip.split(' => ')
      arr
    end
    commands.each do |cmd|
      dowit = cmd.shift
      case dowit
      when 'deploy:'
        deploy cmd.shift
        sleep 1
      when 'modify-deploy:'
        modify_deploy cmd
        sleep 1
      when 'transact:'
        transact cmd.shift
        sleep 1
      when 'query:'
        query cmd.shift
      when 'log:'
        log cmd.shift
      when 'set:'
        set_var cmd.shift
      when 'endow:'
        endow cmd.shift
        sleep 1
      when 'blob:'
        blob cmd.shift
      end
    end
  end
end

#endow(command) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/epm/deploy.rb', line 96

def endow command
  recipient = command.shift
  until ! recipient[/(\{\{.*?\}\})/]
    recipient.gsub!($1, @brain[$1])
  end
  endowment = command.shift
  p "Sending #{recipient} an endowment of: #{endowment}."
  EPM::Transact.new(recipient, [''], @settings).transact endowment
end

#get_remote_if_remoteObject



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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/epm/deploy.rb', line 164

def get_remote_if_remote
  if is_it_a_remote_file?
    begin
      p "Cloning into Remote."
      tmp = Tempfile.new 'epm'
      Dir.chdir File.dirname tmp.path
      unless File.directory? 'cloned'
        Dir.mkdir 'cloned'
      end
      Dir.chdir 'cloned'
      begin
        `git clone #{@def_file}`
      end
      dir = Dir.glob('*')[0]
      dir = File.dirname(tmp.path) + '/cloned/' + dir
      Dir.chdir dir
      def_files = Dir.glob('*').select {|f| File.extname(f) == '.package-definition'}
      if def_files.empty?
        dirs = Dir.glob('*').select {|f| File.directory? f}
        dirs.each do |d|
          Dir.chdir d
          def_files << Dir.glob('*').inject([]) do |arr,f|
            if File.extname(f) == '.package-definition'
              arr << d + '/' + f
            end
            arr
          end
          Dir.chdir dir
        end
      end
      def_files.flatten!
      def_files.each do |df|
        this_def_dir = File.dirname df
        this_def_file = File.basename df
        Dir.chdir this_def_dir
        EPM::Deploy.new(this_def_file, @settings).deploy_package
        Dir.chdir dir
      end
    rescue Exception => e
      p "There was an error during that deploy."
      puts e.message
    ensure
      remote_cleanup tmp
      tmp.unlink
      @def_file = false
    end
  end
end

#is_it_a_remote_file?Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
# File 'lib/epm/deploy.rb', line 213

def is_it_a_remote_file?
  @remote = false
  if @def_file[/https*:\/\//] || @def_file[/gits*:\/\//] || @def_file[/.+@.+\..+:/]
    @remote = true
    return true
  end
  false
end

#log(command) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/epm/deploy.rb', line 122

def log command
  address = command.shift
  name = command.shift
  until ! address[/(\{\{.*?\}\})/]
    address.gsub!($1,@brain[$1])
  end
  until ! name[/(\{\{.*?\}\})/]
    name.gsub!($1,@brain[$1])
  end
  log_entry = [address, name].join(',')
  p "Logging [#{address},#{name}]"
  `echo #{log_entry} >> #{@log_file}`
end

#modify_deploy(command) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/epm/deploy.rb', line 60

def modify_deploy command
  deploy_k = command[0].shift
  deploy_k = EPM::FileHelpers.file_guard(File.join(@dirname, deploy_k))
  k_name = command[0].shift
  dump = command.shift
  until command.empty?
    cmd = command.shift
    to_replace = cmd.shift
    replacer = cmd.shift
    until ! replacer[/(\{\{.*?\}\})/]
      replacer.gsub!($1, @brain[$1])
    end
    k_value = File.read deploy_k
    k_value = k_value.gsub "#{to_replace}", "#{replacer}"
    File.open(deploy_k, 'w'){|f| f.write k_value}
  end
  p "After modifying, am deploying #{deploy_k} with name of #{k_name.gsub(/(\{|\})/,'')}."
  k_address = EPM::Create.new(deploy_k, @settings).deploy
  @brain[k_name] = k_address
  p "#{k_name} => #{k_address}"
end

#query(command) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/epm/deploy.rb', line 106

def query command
  contract = command.shift
  address = command.shift
  name = command.shift
  until ! contract[/(\{\{.*?\}\})/]
    contract.gsub!($1,@brain[$1])
  end
  until ! address[/(\{\{.*?\}\})/]
    address.gsub!($1,@brain[$1])
  end
  p "Querying #{contract} at: #{address}."
  storage = EPM::Query.new(contract, address, @settings).query
  p "#{contract}:#{address} => #{storage}"
  @brain[name] = storage
end

#remote_cleanup(tmp) ⇒ Object



222
223
224
225
226
227
# File 'lib/epm/deploy.rb', line 222

def remote_cleanup tmp
  if @remote
    Dir.chdir File.dirname tmp.path
    FileUtils.rm_rf 'cloned'
  end
end

#set_var(command) ⇒ Object



136
137
138
139
140
141
# File 'lib/epm/deploy.rb', line 136

def set_var command
  key = command.shift
  value = command.shift
  p "Setting #{key} to #{value}"
  @brain[key] = value
end

#setup(settings) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/epm/deploy.rb', line 229

def setup settings
  unless settings.empty?
    @settings = settings
  else
    @settings = EPM::Settings.check
  end
end

#transact(command) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/epm/deploy.rb', line 82

def transact command
  recipient = command.shift
  until ! recipient[/(\{\{.*?\}\})/]
    recipient.gsub!($1, @brain[$1])
  end
  data = command.shift
  until ! data[/(\{\{.*?\}\})/]
    data.gsub!($1,@brain[$1])
  end
  data = data.split(' ')
  p "Sending #{recipient} the data: #{data}."
  EPM::Transact.new(recipient, data, @settings).transact
end