-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from haradan/script-add-user-with-pulumi
Add scripts to create user in Pulumi-deployed droplet
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/bash | ||
|
||
if [ "$#" -ne 2 ]; then | ||
echo "Usage: $0 user_name /path/to/id_ed25519.pub" | ||
exit 1 | ||
fi | ||
|
||
NEW_USERNAME=$1 | ||
PUB_FILE=$2 | ||
|
||
DEPLOY_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd) | ||
STACK=$(pulumi stack --show-name) | ||
DROPLET_IP=$(pulumi stack output droplet_public_ip) | ||
PRIVATE_KEY_FILE="$DEPLOY_DIR/id_cobra_$STACK" | ||
KNOWN_HOSTS_FILE="$DEPLOY_DIR/known_hosts_cobra_$STACK" | ||
CREATE_USER_SCRIPT="$DEPLOY_DIR/bin/in-droplet/create-user-with-key" | ||
|
||
touch "$PRIVATE_KEY_FILE" | ||
chmod u=rw,g=,o= "$PRIVATE_KEY_FILE" | ||
pulumi stack output private_key_openssh --show-secrets > "$PRIVATE_KEY_FILE" | ||
echo "Retrieved connection details" | ||
|
||
echo "Uploading script" | ||
scp -i "$PRIVATE_KEY_FILE" -o "UserKnownHostsFile=$KNOWN_HOSTS_FILE" "$CREATE_USER_SCRIPT" "root@$DROPLET_IP:create-user-with-key" | ||
|
||
echo "Uploading public key" | ||
scp -i "$PRIVATE_KEY_FILE" -o "UserKnownHostsFile=$KNOWN_HOSTS_FILE" "$PUB_FILE" "root@$DROPLET_IP:id.pub" | ||
|
||
echo "Setting script permissions" | ||
ssh -i "$PRIVATE_KEY_FILE" -o "UserKnownHostsFile=$KNOWN_HOSTS_FILE" "root@$DROPLET_IP" chmod 700 ./create-user-with-key | ||
|
||
echo "Creating user" | ||
ssh -i "$PRIVATE_KEY_FILE" -o "UserKnownHostsFile=$KNOWN_HOSTS_FILE" "root@$DROPLET_IP" ./create-user-with-key "$NEW_USERNAME" ./id.pub | ||
|
||
SSH_EXIT_CODE=$? | ||
rm "$PRIVATE_KEY_FILE" | ||
echo "Connect with ssh $NEW_USERNAME@$DROPLET_IP" | ||
exit $SSH_EXIT_CODE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [ "$#" -ne 2 ]; then | ||
echo "Usage: $0 user_name /path/to/id_ed25519.pub" | ||
exit 1 | ||
fi | ||
|
||
NEW_USERNAME=$1 | ||
PUB_FILE=$2 | ||
|
||
adduser --disabled-password --gecos "" "$NEW_USERNAME" | ||
usermod -aG sudo "$NEW_USERNAME" | ||
|
||
NEW_HOME=$(eval echo ~"$NEW_USERNAME") | ||
|
||
mkdir "$NEW_HOME/.ssh" | ||
chown "$NEW_USERNAME:$NEW_USERNAME" "$NEW_HOME/.ssh" | ||
chmod 700 "$NEW_HOME/.ssh" | ||
cat "$PUB_FILE" > "$NEW_HOME/.ssh/authorized_keys" | ||
chown "$NEW_USERNAME:$NEW_USERNAME" "$NEW_HOME/.ssh/authorized_keys" | ||
chmod 600 "$NEW_HOME/.ssh/authorized_keys" |