Class: Anima::Installer

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

Constant Summary collapse

DIRECTORIES =
%w[
  db
  config/credentials
  log
  tmp
  tmp/pids
  tmp/cache
].freeze
ANIMA_HOME =
Pathname.new(File.expand_path("~/.anima")).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anima_home: ANIMA_HOME) ⇒ Installer

Returns a new instance of Installer.



22
23
24
# File 'lib/anima/installer.rb', line 22

def initialize(anima_home: ANIMA_HOME)
  @anima_home = anima_home
end

Instance Attribute Details

#anima_homeObject (readonly)

Returns the value of attribute anima_home.



20
21
22
# File 'lib/anima/installer.rb', line 20

def anima_home
  @anima_home
end

Instance Method Details

#create_config_fileObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/anima/installer.rb', line 45

def create_config_file
  config_path = anima_home.join("config", "anima.yml")
  return if config_path.exist?

  config_path.write(<<~YAML)
    # Anima configuration
    # See https://github.com/hoblin/anima for documentation
  YAML
  say "  created #{config_path}"
end

#create_directoriesObject



35
36
37
38
39
40
41
42
43
# File 'lib/anima/installer.rb', line 35

def create_directories
  DIRECTORIES.each do |dir|
    path = anima_home.join(dir)
    next if path.directory?

    FileUtils.mkdir_p(path)
    say "  created #{path}"
  end
end

#create_systemd_serviceObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/anima/installer.rb', line 83

def create_systemd_service
  service_dir = Pathname.new(File.expand_path("~/.config/systemd/user"))
  service_path = service_dir.join("anima.service")

  return if service_path.exist?

  FileUtils.mkdir_p(service_dir)
  anima_bin = File.join(Gem.bindir, "anima")

  service_path.write(<<~UNIT)
    [Unit]
    Description=Anima - Personal AI Agent
    After=network.target

    [Service]
    Type=simple
    ExecStart=#{anima_bin} start
    Restart=on-failure
    RestartSec=5
    Environment=RAILS_ENV=production

    [Install]
    WantedBy=default.target
  UNIT

  say "  created #{service_path}"
  system("systemctl", "--user", "daemon-reload", err: File::NULL, out: File::NULL)
end

#generate_credentialsObject



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

def generate_credentials
  require "active_support"
  require "active_support/encrypted_configuration"

  %w[production development test].each do |env|
    content_path = anima_home.join("config", "credentials", "#{env}.yml.enc")
    key_path = anima_home.join("config", "credentials", "#{env}.key")

    next if key_path.exist? && content_path.exist?

    key = ActiveSupport::EncryptedFile.generate_key
    key_path.write(key)
    File.chmod(0o600, key_path.to_s)

    config = ActiveSupport::EncryptedConfiguration.new(
      config_path: content_path.to_s,
      key_path: key_path.to_s,
      env_key: "RAILS_MASTER_KEY",
      raise_if_missing_key: true
    )

    config.write("secret_key_base: #{SecureRandom.hex(64)}\n")
    File.chmod(0o600, content_path.to_s)
    say "  created credentials for #{env}"
  end
end

#runObject



26
27
28
29
30
31
32
33
# File 'lib/anima/installer.rb', line 26

def run
  say "Installing Anima to #{anima_home}..."
  create_directories
  create_config_file
  generate_credentials
  create_systemd_service
  say "Installation complete. Run 'anima start' to begin."
end