Skip to content

Commit e47d37b

Browse files
committed
Move key setup to configure hook to work around errors.
1 parent 01bbd63 commit e47d37b

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

snap/hooks/configure

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import configparser
44
import os
5+
import stat
56
import subprocess
67

78

89
SNAP_COMMON = os.environ['SNAP_COMMON']
910
SETTINGS_FILE = "settings.ini"
1011

12+
KEY_DIR_NAME = "keys"
13+
PRIVATE_KEY_FILE = "node.key"
14+
PUBLIC_KEY_FILE = "node.pub"
15+
1116
# List of settings to apply. Each item is a tuple:
1217
# (snap option, ini section, name).
1318
#
@@ -33,7 +38,29 @@ SETTINGS = [
3338
]
3439

3540

36-
if __name__ == "__main__":
41+
def prepare_ssh_key():
42+
key_dir = os.path.join(SNAP_COMMON, KEY_DIR_NAME)
43+
if not os.path.isdir(key_dir):
44+
os.mkdir(key_dir)
45+
46+
private_key_path = os.path.join(key_dir, PRIVATE_KEY_FILE)
47+
if not os.path.isfile(private_key_path):
48+
cmd = ["openssl", "genrsa", "-out", private_key_path, "4096"]
49+
subprocess.call(cmd)
50+
51+
os.chmod(private_key_path, stat.S_IRUSR)
52+
53+
public_key_path = os.path.join(key_dir, PUBLIC_KEY_FILE)
54+
if not os.path.isfile(public_key_path):
55+
cmd = ["ssh-keygen", "-y", "-f", private_key_path]
56+
result = subprocess.check_output(cmd)
57+
pubkey = result.decode('ascii', 'ignore').strip()
58+
59+
with open(public_key_path, "w") as output:
60+
output.write(pubkey)
61+
62+
63+
def prepare_settings_file():
3764
path = os.path.join(SNAP_COMMON, SETTINGS_FILE)
3865

3966
config = configparser.ConfigParser()
@@ -50,7 +77,8 @@ if __name__ == "__main__":
5077
result = subprocess.check_output(cmd)
5178
if len(result) > 0:
5279
value = result.decode('ascii', 'ignore').strip()
53-
config.set(section, name, value)
80+
if len(value) > 0:
81+
config.set(section, name, value)
5482
except subprocess.CalledProcessError:
5583
pass
5684

@@ -65,3 +93,8 @@ if __name__ == "__main__":
6593
config.write(output)
6694
except OSError as error:
6795
print(error)
96+
97+
98+
if __name__ == "__main__":
99+
prepare_ssh_key()
100+
prepare_settings_file()

snap/hooks/install

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,11 @@
55
# unless the snap is completely removed and reinstalled.
66
#
77

8-
9-
KEY_DIR="$SNAP_COMMON/keys"
10-
11-
12-
# Generate a private key for the node. This will be used for SSH access to git
13-
# repositories and other authentication purposes.
14-
if [ ! -f "$KEY_DIR/node.key" ]; then
15-
mkdir -p "$KEY_DIR"
16-
17-
openssl genrsa -out "$KEY_DIR/node.key" 4096
18-
chmod 400 "$KEY_DIR/node.key"
19-
20-
ssh-keygen -y -f "$KEY_DIR/node.key" >"$KEY_DIR/node.pub"
21-
fi
22-
8+
# Warning: do not touch files or directories in the SNAP_COMMON directory using
9+
# this install script. Even though the install script would seem like a logical
10+
# place to initialize that directory, it seems to interact strangely with
11+
# snap's unique filesystem layout. I believe this was the cause of write
12+
# failures with a "read-only filesystem" error message.
2313

2414
# Initialize snap settings.
2515
#

0 commit comments

Comments
 (0)