aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm-chandler <mchandler89@gmail.com>2024-10-14 08:35:58 +1000
committerGitHub <noreply@github.com>2024-10-14 08:35:58 +1000
commit9ad64993477c849da2e487b0c8c3034ff9eadb88 (patch)
tree3c6b1029073547e962761f7ce94d1ebb5dded2ce
parentfe59339df077f7c4a02aad744ff136e57ae60176 (diff)
parent3e68275ca3e72895c7af53d788eaa4e8d2c9889c (diff)
Merge pull request #36 from robertmassaioli/bring-in-utility-scripts
Bring in utility scripts
-rw-r--r--.gitignore2
-rw-r--r--readme.md26
-rw-r--r--util/delete-filesystems.bash17
-rw-r--r--util/download-latest-save.bash67
-rw-r--r--util/upload-save.bash52
5 files changed, 163 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d747ddd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.sw[a-p]
+*.zip \ No newline at end of file
diff --git a/readme.md b/readme.md
index bffca61..1e99e2e 100644
--- a/readme.md
+++ b/readme.md
@@ -55,7 +55,31 @@ For remote access, you'll need to:
If you're creating a new Factorio deployment, provide these parameters when creating the stack. Otherwise, update your existing stack and provide these parameters.
-#### Uploading an existing save.
+#### Uploading and Downloading an existing save.
+
+##### Fast save upload (Recommended)
+
+Warning: Makes sure that your server is live and all EC2 and ECS healthchecks are green before trying this.
+
+Use the automation in `util/upload-save.bash` to upload your save file to your server, like so:
+
+``` bash
+bash util/upload-save.bash ~/path/to/my/save.zip $your_ec2_ip_or_remote_name
+```
+
+This is just an automated implementation of the slower version below.
+
+##### Fast save download (Recommended)
+
+Use the automation in `util/download-latest-save.bash` to download the latest (most recently played) save from a factorio server:
+
+``` bash
+bash util/download-latest-save.bash $your_ec2_ip_or_remote_name
+```
+
+Your server needs to be running for this to work and it should download your latest save to your local directory.
+
+##### Manual upload process (for understanding the system)
This procedure involves uploading your new save and then force killing the docker container. When the container is force killed it won't auto save, and the default logic is that on restart, the latest save will be loaded. To do this you must have SSH enabled via the CloudFormation deployment. The container must be running, otherwise you can't access EFS (where the save resides) from the EC2 instance.
diff --git a/util/delete-filesystems.bash b/util/delete-filesystems.bash
new file mode 100644
index 0000000..dc5bbbd
--- /dev/null
+++ b/util/delete-filesystems.bash
@@ -0,0 +1,17 @@
+# Get all of the filesystems to delete
+# aws efs describe-file-systems --query "FileSystems[?Name!=null]|[?starts_with(Name, 'factorio-')].FileSystemId" --output text | cat
+
+# For each filesystem whose name starts with "factorio-" delete it.
+# This is a very very dangerous script. Only use it if you know what you are doing.
+# I recommend you download your save files first using download-latest-save.bash
+
+for fs_id in $(aws efs describe-file-systems --query "FileSystems[?Name!=null]|[?starts_with(Name, 'factorio-')].FileSystemId" --output text | cat)
+do
+ echo "Deleting file system: $fs_id"
+ aws efs delete-file-system --file-system-id $fs_id
+ if [ $? -eq 0 ]; then
+ echo "Successfully deleted file system: $fs_id"
+ else
+ echo "Failed to delete file system: $fs_id"
+ fi
+done \ No newline at end of file
diff --git a/util/download-latest-save.bash b/util/download-latest-save.bash
new file mode 100644
index 0000000..2a84660
--- /dev/null
+++ b/util/download-latest-save.bash
@@ -0,0 +1,67 @@
+#!/bin/bash
+
+# Check if remote name is provided
+if [ $# -ne 1 ]; then
+ echo "Usage: $0 <remote_name>"
+ exit 1
+fi
+
+remote_name="$1"
+
+# Generate a human-readable timestamp
+timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
+
+# SSH into the remote instance to find the most recent save file
+ssh_output=$(ssh "ec2-user@$remote_name" << EOF
+ # Record the current directory
+ current_dir=\$(pwd)
+
+ # Find the save directory
+ savedir=\$(sudo mount | grep nfs4 | cut -f3 -d ' ' | xargs -I {} echo "{}/saves")
+ echo "Save directory: \$savedir"
+
+ if [ -z "\$savedir" ]; then
+ echo "ERROR: Save directory not found"
+ exit 1
+ fi
+
+ # Find the most recently modified file in the save directory
+ latest_file=\$(sudo ls -t \$savedir | head -1)
+
+ if [ -z "\$latest_file" ]; then
+ echo "ERROR: No files found in the save directory"
+ exit 1
+ fi
+
+ echo "Latest save file: \$latest_file"
+
+ # Copy the latest file to the current directory
+ sudo cp "\$savedir/\$latest_file" "\$current_dir/"
+
+ # Change ownership of the copied file to ec2-user
+ sudo chown ec2-user:ec2-user "\$current_dir/\$latest_file"
+
+ echo "\$current_dir/\$latest_file"
+EOF
+)
+
+# Check if there was an error in the SSH command
+if echo "$ssh_output" | grep -q "ERROR:"; then
+ echo "$ssh_output"
+ exit 1
+fi
+
+# Extract the full path of the latest file
+latest_file_path=$(echo "$ssh_output" | tail -n 1)
+
+# Extract just the filename
+latest_file=$(basename "$latest_file_path")
+
+# Download the file from the remote instance to the current local directory with the new filename
+new_filename="${remote_name}_${timestamp}_${latest_file}"
+scp "ec2-user@$remote_name:$latest_file_path" "./$new_filename"
+
+# Clean up the temporary file on the remote instance
+ssh "ec2-user@$remote_name" "rm -f $latest_file_path"
+
+echo "Download complete. The latest save file has been saved as '$new_filename' in your current directory." \ No newline at end of file
diff --git a/util/upload-save.bash b/util/upload-save.bash
new file mode 100644
index 0000000..9c66030
--- /dev/null
+++ b/util/upload-save.bash
@@ -0,0 +1,52 @@
+#!/bin/bash
+
+# Check if both arguments are provided
+if [ $# -ne 2 ]; then
+ echo "Usage: $0 <path_to_MySave.zip> <ec2_address>"
+ exit 1
+fi
+
+# Get the file path and EC2 address from command line arguments
+save_file="$1"
+ec2_address="$2"
+
+# Check if the file exists
+if [ ! -f "$save_file" ]; then
+ echo "File not found: $save_file"
+ exit 1
+fi
+
+# Upload the save file to the EC2 instance
+echo "Uploading save file to EC2 instance..."
+scp "$save_file" "ec2-user@$ec2_address:~/"
+
+# SSH into the EC2 instance and perform the required operations
+ssh "ec2-user@$ec2_address" << EOF
+ # Get the Factorio container ID
+ container_id=\$(docker ps | grep factoriotools/factorio | awk '{print \$1}' | cut -c1-3)
+
+ if [ -z "\$container_id" ]; then
+ echo "Factorio container not found"
+ exit 1
+ fi
+
+ echo "Factorio container ID: \$container_id"
+
+ # Find the save directory
+ savedir=\$(mount | grep nfs4 | cut -f3 -d ' ' | xargs -I {} echo "{}/saves")
+ echo "Save directory: \$savedir"
+
+ # Move the uploaded save to the right location
+ sudo mv ~/$(basename "$save_file") \$savedir
+
+ # Touch the save file to update its timestamp
+ sudo touch \$savedir/$(basename "$save_file")
+
+ # Force kill the Factorio docker container
+ echo "Killing Factorio container..."
+ docker kill \$container_id
+
+ echo "Save file uploaded and container restarted. Please wait 30 seconds for the server to come back online."
+EOF
+
+echo "Script completed. The server should load your new save file when it restarts." \ No newline at end of file
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage