Class: Metaverse::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/metaverse/base.rb', line 11

def initialize path
  @logger = Logger.new STDOUT
  @base_path = path
  read_config "#{path}/.meta.yml"
  @repos_paths = Metaverse::Iterator.new path, @ignored_repos, @repos_paths
  if @repos_paths.empty?
    @repos_paths.build 
    save_config "#{path}/.meta.yml"
    puts "Repos cache built.".green
  end      
  @repos = @repos_paths.map {|repo| Metaverse::Repo.new repo}
end

Instance Method Details

#add_remote(name, base_url) ⇒ Object



192
193
194
195
196
197
# File 'lib/metaverse/base.rb', line 192

def add_remote name, base_url
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.add_remote name, "#{base_url}/#{repo.name}"
  }
end

#branchesObject



117
118
119
# File 'lib/metaverse/base.rb', line 117

def branches
  @repos.each { |repo| puts "#{repo.name}@#{repo.current_branch}" }
end

#check_consistencyObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/metaverse/base.rb', line 143

def check_consistency
  branch_name = system_state.first
  inconsistent_repos = @repos.reject { |repo|
    repo.current_branch == branch_name
  }.map &:name

  if inconsistent_repos.length == 0
    puts 'The system is consistent'.green
  else
    puts 'The system is inconsistent. Please check the following repos :'.red , inconsistent_repos
  end
  inconsistent_repos.length == 0 
end

#check_dirtinessObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/metaverse/base.rb', line 130

def check_dirtiness
  dirty_repos = @repos.reject { |repo|
    !repo.dirty?
  }.map &:name

  if dirty_repos.length == 0
    puts 'The system is clean'.green
  else
    puts 'The system is dirty. Please check the following repos :'.red , dirty_repos
  end
  dirty_repos.length == 0 
end

#checkout(name) ⇒ Object



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
# File 'lib/metaverse/base.rb', line 30

def checkout name
  action_options = -> (repo) {
    {
      up: -> {
        puts "\n # #{repo.name}".blue
        repo.checkout name
      },
      down: -> {
        puts "\n # Rolling back in #{repo.name}".blue
        repo.checkout repo.pop_previous_branch
      }
    }
  }

  if check_dirtiness and check_consistency
    actions = @repos.map {|repo| 
      puts "\n # #{repo.name}".blue
      Transacted::Action.new action_options.call(repo)
    }

    checkout_transaction = Transacted::Transaction.new actions
    case checkout_transaction.execute
    when :execution_success then puts "Checkout successful".green
    when :rollback_success then puts "An error prevented checking out the system. Rolled back successfully".yellow
    when :rollback_failure then puts "An error was encountered during the checkout. In addition, an error happened while trying to rollback the system. Please fix the state of your system manually.".red
    end
  end
    
end

#clear_reposObject



179
180
181
182
183
# File 'lib/metaverse/base.rb', line 179

def clear_repos
  @repos_paths = []
  save_config "#{@base_path}/.meta.yml"
  puts "Repos cache cleared.".green
end

#create_state(prefix, state) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/metaverse/base.rb', line 60

def create_state prefix, state
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.create_state prefix, state
    repo.checkout "#{prefix}/#{state}" if not prefix == "snapshot"
  } if check_dirtiness and check_consistency
end

#exec(env, command) ⇒ Object



199
200
201
202
203
204
# File 'lib/metaverse/base.rb', line 199

def exec env, command
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.exec env, command
  }
end

#load_state(prefix, state, remote = nil, should_create_branch = false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/metaverse/base.rb', line 69

def load_state prefix, state, remote = nil, should_create_branch = false
  action_options = -> (repo) {
    {
      up: -> { 
        puts "\n # #{repo.name}".blue
        repo.load_state prefix, state, remote, should_create_branch
      },
      down: -> { 
        puts "\n # Rolling back in #{repo.name}".blue
        repo.checkout repo.peek_previous_branch
        repo.delete_branch repo.pop_previous_branch if should_create_branch
      }
    }
  }

  if check_dirtiness and check_consistency
    
    actions = @repos.map { |repo|
      Transacted::Action.new action_options.call repo
    }

    load_state_transaction = Transacted::Transaction.new actions
    case load_state_transaction.execute
    when :execution_success then puts "Loading state successful".green
    when :rollback_success then puts "An error prevented loading the state of the system. Rolled back successfully".yellow
    when :rollback_failure then puts "An error was encountered during the loading of the state. In addition, an error happened while trying to rollback the system. Please fix the state of your system manually.".red
    end
  end
end

#pullObject



185
186
187
188
189
190
# File 'lib/metaverse/base.rb', line 185

def pull
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.pull @origin_remote
  } if check_dirtiness and check_consistency
end

#read_config(path) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/metaverse/base.rb', line 157

def read_config path
  Errors::config_not_found! if not File.exist? path

  config = YAML.load File.open(path)
  @repos_paths = config['repos'] || []
  @origin_remote = config['remotes']['main']
  @own_remote = config['remotes']['own']
  @ignored_repos = config['ignore'] || []
end

#save_config(path) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/metaverse/base.rb', line 167

def save_config path
  config = {
    'repos' => @repos_paths.map{ |repo| repo },
    'remotes'=> {
      'main' => @origin_remote,
      'own' => @own_remote
    },
    'ignore' => @ignored_repos
  }
  File.open(path, 'w') {|f| f.write config.to_yaml }
end

#send_state(prefix, state, remote, should_clean = false) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/metaverse/base.rb', line 100

def send_state prefix, state, remote, should_clean = false
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    is_branch = repo.current_branch.match /refs\/heads\/(.*)/
    repo.send_state prefix, state, remote, should_clean
  } if check_dirtiness and check_consistency
end

#statusObject



24
25
26
27
# File 'lib/metaverse/base.rb', line 24

def status
  check_dirtiness
  check_consistency
end

#system_stateObject



121
122
123
124
125
126
127
128
# File 'lib/metaverse/base.rb', line 121

def system_state
  branches = Hash.new 0
  @repos.each {|repo| branches[repo.current_branch] += 1}

  state = branches.max{|a,b| a[1] <=> b[1]}
  state[0] = state.first.strip
  state
end

#update(remote = nil) ⇒ Object



109
110
111
112
113
114
# File 'lib/metaverse/base.rb', line 109

def update remote = nil
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.update remote
  } if check_dirtiness and check_consistency
end