File size: 2,398 Bytes
aa90f60
 
 
 
 
 
23a50be
2444678
 
 
 
 
 
 
 
 
aa90f60
23a50be
aa90f60
23a50be
aa90f60
2444678
 
aa90f60
 
 
 
2444678
aa90f60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23a50be
aa90f60
 
1
2
3
4
5
6
7
8
9
10
11
12
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
50
51
52
53
54
55
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
82
83
84
85
86
87
import subprocess
import os
import time
from huggingface_hub import HfApi

def start_mastodon():
    # Ensure secrets are available
    required_secrets = [
        "SECRET_KEY_BASE",
        "OTP_SECRET",
        "VAPID_PRIVATE_KEY",
        "VAPID_PUBLIC_KEY"
    ]
    for secret in required_secrets:
        if not os.getenv(secret):
            raise ValueError(f"Missing required secret: {secret}")

    # Set environment variables, use port 7860
    os.environ["RAILS_ENV"] = "development"
    os.environ["PORT"] = "7860"  # Changed from 3000 to 7860
    os.environ["BIND"] = "0.0.0.0"
    os.environ["LOCAL_DOMAIN"] = os.getenv("LOCAL_DOMAIN", "localhost")
    os.environ["RAILS_SERVE_STATIC_FILES"] = os.getenv("RAILS_SERVE_STATIC_FILES", "true")

    # Change to Mastodon directory
    os.chdir("/app/mastodon")

    # Patch Mastodon to use custom storage
    with open("config/initializers/custom_storage.rb", "w") as f:
        f.write("""
# Custom storage initializer
require 'open3'

module CustomStorage
  def self.call_python(action, *args)
    cmd = ['python3', '/app/mastodon/mastodon_config.py', action, *args]
    stdout, stderr, status = Open3.capture3(*cmd)
    stdout.strip if status.success?
  end
end

# Override User model (example)
class User
  def self.find_by_username(username)
    data = CustomStorage.call_python('get_user', username)
    data ? new(JSON.parse(data)) : nil
  end

  def save
    CustomStorage.call_python('save_user', username, to_json)
  end
end

# Override Status model (example)
class Status
  def save
    CustomStorage.call_python('save_post', id.to_s, to_json)
  end

  def self.find(id)
    data = CustomStorage.call_python('get_post', id.to_s)
    data ? new(JSON.parse(data)) : nil
  end
end
""")

    # Start Puma server
    process = subprocess.Popen(
        ["/root/.rbenv/shims/bundle", "exec", "puma", "-C", "config/puma.rb"],
        env=os.environ,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )

    # Stream logs
    while True:
        output = process.stdout.readline()
        if output == b'' and process.poll() is not None:
            break
        if output:
            print(output.decode().strip())
    return process

if __name__ == "__main__":
    print("Starting Mastodon with Hugging Face Secrets and Storage on port 7860...")
    mastodon_process = start_mastodon()
    mastodon_process.wait()