File size: 3,110 Bytes
ee0ac8c
 
 
 
 
 
 
5b8ed2b
 
 
 
 
 
 
ee0ac8c
 
 
 
 
 
 
 
 
5b8ed2b
ee0ac8c
 
 
 
 
 
 
 
 
 
 
 
5b8ed2b
ee0ac8c
 
5b8ed2b
ee0ac8c
 
 
5b8ed2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee0ac8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b8ed2b
ee0ac8c
 
 
 
5b8ed2b
 
ee0ac8c
 
 
 
 
 
 
 
5b8ed2b
 
 
 
 
 
 
 
 
ee0ac8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b8ed2b
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh

BASE=/home/node/app
USERNAME=$(printenv username)
PASSWORD=$(printenv password)

function env() {
  if [[ ! -z "${fetch}" ]]; then
    echo '远程获取参数...'
    curl -s "$fetch" -o data.json
    export github_secret=$(jq -r .github_secret data.json)
    export github_project=$(jq -r .github_project data.json)
  fi

  if [[ -z "${USERNAME}" ]]; then
    USERNAME="root"
  fi

  if [[ -z "${PASSWORD}" ]]; then
    PASSWORD="123456"
  fi

  echo
  echo "fetch = ${fetch}"
  echo "github_secret = $github_secret"
  echo "github_project = $github_project"
  echo "USERNAME = ${USERNAME}"
  echo "PASSWORD = ${PASSWORD}"
  echo
  echo

  sed -i "s/\[github_secret\]/${github_secret}/g" launch.sh
  sed -i "s#\[github_project\]#${github_project}#g" launch.sh
}

function init() {
  mkdir -p ${BASE}/history
  cd ${BASE}/history

  # Configure git
  git config --global user.email "huggingface@hf.com"
  git config --global user.name "complete-Mmx"
  git config --global init.defaultBranch main
  
  # Check if repository exists
  if [ ! -d ".git" ]; then
    echo "Initializing new repository..."
    git init
    git remote add origin https://[github_secret]@github.com/[github_project].git
    
    # Try to fetch with shallow clone first
    if ! git fetch --depth 1 origin main; then
      echo "Shallow clone failed, trying full clone..."
      git pull origin main
    fi
  else
    echo "Repository exists, checking for updates..."
    # Only pull if there are changes
    git fetch origin main
    if [ "$(git rev-parse HEAD)" != "$(git rev-parse origin/main)" ]; then
      echo "Changes detected, pulling updates..."
      git pull origin main
    else
      echo "No changes detected, skipping pull."
    fi
  fi

  cd ${BASE}

  DIR="${BASE}/history"
  if [ "$(ls -A $DIR | grep -v .git)" ]; then
    echo "Has history..."
  else
    echo "Empty history..."
    cp -r data/* history/
    cp -r secrets.json history/secrets.json
  fi

  rm -rf data
  ln -s history data

  rm -r config.yaml
  cp config/config.yaml history/config.yaml
  ln -s history/config.yaml config.yaml
  sed -i "s/username: .*/username: \"${USERNAME}\"/" ${BASE}/config.yaml
  sed -i "s/password: .*/password: \"${PASSWORD}\"/" ${BASE}/config.yaml
  sed -i "s/whitelistMode: true/whitelistMode: false/" ${BASE}/config.yaml
  sed -i "s/basicAuthMode: false/basicAuthMode: true/" ${BASE}/config.yaml
  cat config.yaml
  echo "Init history."
  chmod -R 777 history

  # Start git-batch with optimized settings
  nohup ./git-batch --commit 30s --name git-batch --email git-batch@github.com --push 2m -p history > access.log 2>1 &
}

function release() {
  rm -rf history
}

function update() {
  cd ${BASE}/history
  # Only push if there are changes
  if [ -n "$(git status --porcelain)" ]; then
    git add .
    echo "'update history$(date "+%Y-%m-%d %H:%M:%S")'"
    git commit -m "'update history$(date "+%Y-%m-%d %H:%M:%S")'"
    git push origin main
  else
    echo "No changes to commit."
  fi
}

case $1 in
  env)
    env
  ;;
  init)
    init
  ;;
  release)
    release
  ;;
  update)
    update
  ;;
esac