first commit

This commit is contained in:
Aleksander Cynarski 2017-04-13 21:54:20 +02:00
parent 765a1bc5be
commit d3ba30648e

72
autoinit Executable file
View File

@ -0,0 +1,72 @@
#!/bin/bash
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/
ssh-keygen -f $HOME/.ssh/gogs -t rsa -N ''
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
title=$(hostname)@$(whoami)
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
fi