Class: Coffer::Installer

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

Constant Summary collapse

CACHE_DIR =
'/opt/coffer/cache'
BIN_DIR =
'/opt/coffer/bin'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coin) ⇒ Installer

Returns a new instance of Installer.



11
12
13
# File 'lib/coffer/installer.rb', line 11

def initialize( coin )
  @coin = coin
end

Instance Attribute Details

#coinObject (readonly)

Returns the value of attribute coin.



9
10
11
# File 'lib/coffer/installer.rb', line 9

def coin
  @coin
end

Instance Method Details

#built_executable_pathObject



175
176
177
# File 'lib/coffer/installer.rb', line 175

def built_executable_path
  File.join repo_path, 'src', @coin.wallet_executable
end

#built_executable_valid?Boolean

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/coffer/installer.rb', line 179

def built_executable_valid?
  File.exists?( built_executable_path ) \
    && File.executable?( built_executable_path )
end

#check_resultsObject



118
119
120
# File 'lib/coffer/installer.rb', line 118

def check_results
  raise "Build failed. No executable!" unless built_executable_valid?
end

#cloneObject

clone a repo to the correct directory if the directory already exists and is a git repo then do nothing.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coffer/installer.rb', line 43

def clone
  return false if File.directory?( repo_path )

  warn "Cloning repo... #{repo_path}"

  g = Git.clone( coin.git_repo, File.basename(repo_path), :path => File.dirname(repo_path) )

  g.checkout @coin.git_branch

  true
end

#config_pathObject



110
111
112
# File 'lib/coffer/installer.rb', line 110

def config_path
  File.join( wallet_home_directory, @coin.config_file )
end

#copy_built_executableObject



85
86
87
# File 'lib/coffer/installer.rb', line 85

def copy_built_executable
  FileUtils.cp built_executable_path, installed_executable_path
end

#create_configObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/coffer/installer.rb', line 97

def create_config
  if File.exists?( config_path )
    puts "Not creating config. (#{ config_path })"
    return
  end

  File.open( config_path , 'w' ) do |f|
    f.write @coin.build_config.to_config
  end

  FileUtils.chmod 0600, config_path
end

#create_directoriesObject



89
90
91
# File 'lib/coffer/installer.rb', line 89

def create_directories
  mkdir wallet_home_directory, 0700
end

#installObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/coffer/installer.rb', line 15

def install
  mkdir CACHE_DIR, 0755
  mkdir BIN_DIR, 0755

  update_repo
  # preconfigure

  Dir.chdir( File.join( repo_path, @coin.build_dir) ) do
    mkdir 'obj', 0755
    make

    check_results
  end

  copy_built_executable
  create_directories
  create_config
end

#installed_executable_pathObject



171
172
173
# File 'lib/coffer/installer.rb', line 171

def installed_executable_path
  File.join BIN_DIR, @coin.wallet_executable
end

#makeObject



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
# File 'lib/coffer/installer.rb', line 122

def make
  if @coin.build.nil?
    cmd = "make -f '#{ makefile_name }'"

    puts `#{cmd}`

    retval = $?
    if retval != 0
      warn "ERROR from command: (#{ cmd }): #{ retval }"
    end
  else
    @coin.build.split("\n").each do |cmd|
      cmd = cmd.strip
      puts "EXEC> #{ cmd }"
      puts `#{cmd}`

      retval = $?
      if retval != 0
        warn "ERROR from command (#{cmd}): #{ retval }"
        return false
      end
    end
  end

  true
end

#makefile_nameObject



149
150
151
152
153
154
155
156
157
# File 'lib/coffer/installer.rb', line 149

def makefile_name
  sys = `uname`.chomp.downcase

  if sys == 'darwin'
    'makefile.osx'
  elsif sys == 'linux'
    'makefile.unix'
  end
end

#mkdir(path, mode = 0700) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/coffer/installer.rb', line 159

def mkdir( path, mode=0700 )
  begin
    FileUtils.mkdir_p path, :mode => mode
  rescue Errno::EEXIST
    #pass
  end
end

#pullObject

pull the latest changes and ensure we have the correct ref checked out.



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

def pull
  raise "Not a git repository! (#{ repo_path })" unless File.directory?( repo_path )

  warn "Updating repo... #{repo_path}"

  g = Git.open( repo_path, :logger => Logger.new(STDOUT) )

  # store the current SHA1
  old_sha = g.object('HEAD').sha

  # ensure the remote is configured as expected
  if g.remote.url != coin.git_repo
    g.remove_remote 'origin'
    g.add_remote 'origin', coin.git_repo, :fetch => true
  end

  # make sure the tree is clean
  g.reset_hard
  g.clean :force => true, :d => true

  # fetch data from remote, checkout the right branch and merge into local branch
  g.checkout @coin.git_branch
  g.pull('origin', @coin.git_branch)

  # check if there are any changes.
  old_sha != g.object('HEAD').sha
end

#repo_pathObject



167
168
169
# File 'lib/coffer/installer.rb', line 167

def repo_path
  File.join CACHE_DIR, @coin.name
end

#startObject



114
115
116
# File 'lib/coffer/installer.rb', line 114

def start
  `#{ installed_executable_path } -daemon`
end

#update_repoObject

ensure that this git repo is running the latest version and is cloned and all that



36
37
38
# File 'lib/coffer/installer.rb', line 36

def update_repo
  [ clone, pull ].count { |i| i } > 0
end

#wallet_home_directoryObject



93
94
95
# File 'lib/coffer/installer.rb', line 93

def wallet_home_directory
  File.expand_path("~/.#{ @coin.directory }")
end