mygogs/autoinit

81 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

2017-04-13 19:54:20 +00:00
#!/bin/bash
2017-04-13 20:19:44 +00:00
rname=$(date +%s | sha256sum | base64 | head -c 32)
2017-04-13 19:54:20 +00:00
if [ -z $GOGS_ROOT_URL ]
then
printf "Need to set GOGS_ROOT_URL environment variable\n" >&2
printf "\tIE: https://git.cynarski.pl\n" >&2
exit
fi
if [ -z $GOGS_TOKEN ]
then
printf "Need to set GOGS_TOKEN environment variable\n" >&2
printf "\tThis can be obtained at $GOGS_ROOT_URL/user/settings/applications\n" >&2
exit
fi
if [ -z $GOGS_USER ]
then
printf "Need to set GOGS_USER environment variable\n" >&2
exit
fi
if [ -z $GOGS_REPO ]
then
printf "Need to set GOGS_REPO environment variable\n" >&2
exit
fi
if [ -f $HOME/.ssh/gogs.pub ]; then
printf "Gogs deploy key is present\n" >&2
else
mkdir -p $HOME/.ssh/
2017-04-13 20:25:46 +00:00
ssh-keygen -f $HOME/.ssh/gogs
2017-04-13 20:14:09 +00:00
chmod 400 $HOME/.ssh/gogs
2017-04-13 20:21:52 +00:00
chmod 400 $HOME/.ssh/gogs.pub
2017-04-13 19:54:20 +00:00
fi
prefix="https://"
sshhost=${GOGS_ROOT_URL#$prefix}
sbody=$(cat <<-END
Host $sshhost
IdentityFile ~/.ssh/gogs.pub
END
)
if [ -f $HOME/.ssh/$GOGS_REPO-sended ]; then
printf "Key sended\n" >&2
else
printf "Prepare to send key\n" >&2
2017-04-13 20:19:44 +00:00
title=$rname@$(hostname)
2017-04-13 19:54:20 +00:00
printf "title $title\n" >&2
deploykey=$(cat $HOME/.ssh/gogs.pub)
body=$(cat <<-END
{
"title": "$title",
"key": "$deploykey"
}
END
)
prefix="https://"
sshhost=${GOGS_ROOT_URL#$prefix}
curl -H "Content-Type: application/json" \
-H "Authorization: token $GOGS_TOKEN" \
-d "$body" \
$GOGS_ROOT_URL/api/v1/repos/$GOGS_USER/$GOGS_REPO/keys > /dev/null
touch $HOME/.ssh/$GOGS_REPO-sended
echo "$sbody" >> $HOME/.ssh/config
2017-04-13 20:06:58 +00:00
echo "Almost done. Enter clone repository target dir [ENTER]"
read cdir
2017-04-13 20:08:50 +00:00
git clone ssh://gogs@$sshhost:65522/$GOGS_USER/$GOGS_REPO $cdir
2017-04-13 19:54:20 +00:00
fi