Class: DevboxLauncher::MutagenSession

Inherits:
Object
  • Object
show all
Defined in:
lib/devbox_launcher/models/mutagen_session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, config:, hostname:) ⇒ MutagenSession

Returns a new instance of MutagenSession.



6
7
8
9
10
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 6

def initialize(label:, config:, hostname:)
  @label = label
  @config = config
  @hostname = hostname
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 4

def config
  @config
end

#hostname ⇒ Object (readonly)

Returns the value of attribute hostname.



4
5
6
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 4

def hostname
  @hostname
end

#label ⇒ Object (readonly)

Returns the value of attribute label.



4
5
6
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 4

def label
  @label
end

Instance Method Details

#create ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 12

def create
  puts "Create mutagen session syncing local " \
    "#{config.alpha_dir} with " \
    "#{hostname} #{config.beta_dir}"

  create_stdout,
    create_stderr,
    create_status =
    Open3.capture3(create_cmd)

  if not create_status.success?
    # mutagen prints to stdout and stderr
    msg = "Failed to create mutagen sessions: " \
      "#{create_stdout} -" \
      "#{create_stderr}"
    fail msg
  end
end

#create_cmd ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 35

def create_cmd
  str = [
    "mutagen sync create",
    config.alpha_dir,
    "#{hostname}:#{config.beta_dir}",
    "--label=#{label}",
    "--sync-mode=two-way-resolved",
    "--ignore-vcs",
    "--ignore=.DS_Store"
  ]
  str << "--watch-mode-alpha=no-watch" if linux?
  str.join(" ")
end

#linux? ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 31

def linux?
  OS.linux?
end

#reset ⇒ Object



77
78
79
80
81
82
83
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 77

def reset
  return if !config.configured?

  terminate
  create
  watch_alpha if linux?
end

#terminate ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 53

def terminate
  puts "Terminating mutagen session..."
  terminate_stdout,
    terminate_stderr,
    terminate_status =
    Open3.capture3(terminate_cmd)

  if not terminate_status.success?
    # mutagen prints to stdout and stderr
    msg = "Failed to terminate mutagen sessions: " \
      "#{terminate_stdout} -" \
      "#{terminate_stderr}"
    fail msg
  end
end

#terminate_cmd ⇒ Object



49
50
51
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 49

def terminate_cmd
  %Q(mutagen sync terminate --label-selector=#{label})
end

#watch_alpha ⇒ Object



69
70
71
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 69

def watch_alpha
  watchman.trigger("mutagen sync flush --label-selector=#{label}")
end

#watchman ⇒ Object



73
74
75
# File 'lib/devbox_launcher/models/mutagen_session.rb', line 73

def watchman
  @watchman ||= Watchman.new(dir: config.alpha_dir)
end