Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall
diff options
context:
space:
mode:
authorYash Tripathi <tripathiyash97@gmail.com>2021-05-20 01:01:58 +0530
committerGitHub <noreply@github.com>2021-05-19 21:31:58 +0200
commitbbb4599165a644bbd81b085fb3210cd0e497d503 (patch)
tree3ba10fc59c955032ff948571cdbce158bbdf3ec2 /archinstall
parent49e6cbdc545402e066bdc2daf6054abf6c1bf977 (diff)
Added support for getting configuration from a config file (#364)
* added support for ingesting config * fixed condition to check key in dictionary * Removed redundant code, profile and desktop keys are now optional * Added base-config.json and support for pulling credentials from .env * added base config file and env file for users credentials * added silent install switch * added python-dotenv as a dependency * Updated Readme to include argparse changes as well as config ingestion * Updated Readme to include argparse changes as well as config ingestion * fixed typo in pyproject.toml * Replaced the magic __builtin__ global variable. This should fix mypy complaints while still retaining the same functionality, kinda. It's less automatic but it's also less of dark magic, which makes sense for anyone but me. * Fixes string index error. * Quotation error. * fixed initializing --script argument * added python-dotenv as a dependency * Installation can't be silent if config is not passed * fixed silent install help * fixed condition for ask_user_questions * reverted to creating profile object properly * Cleaned up and incorporated suggestions * added Profile import * added condition if Profile is null * fixed condition * updated parsing vars from argparse * removed loading users from .env * Reworking SysCommand & Moving to localectl for locale related activities (#4) * Moving to `localectl` rather than local file manipulation *(both for listing locales and setting them)*. * Swapped `loadkeys` for localectl. * Renamed `main` to `maim` in awesome profile. * Created `archinstall.Boot(<installation>)` which spawns a `systemd-nspawn` container against the installation target. * Exposing systemd.py's internals to archinstall global scope. * Re-worked `SysCommand` completely, it's now a wrapper for `SysCommandWorker` which supports interacting with the process in a different way. `SysCommand` should behave just like the old one, for backwards compatibility reasons. This fixes #68 and #69. * `SysCommand()` now has a `.decode()` function that defaults to `UTF-8`. * Adding back peak_output=True to pacstrap. Co-authored-by: Anton Hvornum <anton.feeds@gmail.com> Co-authored-by: Dylan Taylor <dylan@dylanmtaylor.com> Co-authored-by: Anton Hvornum <anton@hvornum.se> Co-authored-by: Anton Hvornum <anton.feeds@gmail.com> * fixed indent * removed redundant import * removed duplicate import * removed duplicate import Co-authored-by: Anton Hvornum <anton.feeds@gmail.com> Co-authored-by: Anton Hvornum <anton@hvornum.se> Co-authored-by: Dylan M. Taylor <dylan@dylanmtaylor.com>
Diffstat (limited to 'archinstall')
-rw-r--r--archinstall/__init__.py60
-rw-r--r--archinstall/lib/disk.py2
-rw-r--r--archinstall/lib/general.py3
-rw-r--r--archinstall/lib/networking.py4
-rw-r--r--archinstall/lib/systemd.py9
5 files changed, 51 insertions, 27 deletions
diff --git a/archinstall/__init__.py b/archinstall/__init__.py
index 075b6f50..276d122f 100644
--- a/archinstall/__init__.py
+++ b/archinstall/__init__.py
@@ -1,4 +1,6 @@
"""Arch Linux installer - guided, templates etc."""
+from argparse import ArgumentParser, FileType
+
from .lib.disk import *
from .lib.exceptions import *
from .lib.general import *
@@ -16,22 +18,46 @@ from .lib.storage import *
from .lib.systemd import *
from .lib.user_interaction import *
+parser = ArgumentParser()
+
__version__ = "2.2.0.dev1"
-# Basic version of arg.parse() supporting:
-# --key=value
-# --boolean
-arguments = {}
-positionals = []
-for arg in sys.argv[1:]:
- if '--' == arg[:2]:
- if '=' in arg:
- key, val = [x.strip() for x in arg[2:].split('=', 1)]
- else:
- key, val = arg[2:], True
- arguments[key] = val
- else:
- positionals.append(arg)
+
+def initialize_arguments():
+ config = {}
+ parser.add_argument("--config", nargs="?", help="json config file", type=FileType("r", encoding="UTF-8"))
+ parser.add_argument("--silent", action="store_true",
+ help="Warning!!! No prompts, ignored if config is not passed")
+ parser.add_argument("--script", default="guided", nargs="?", help="Script to run for installation", type=str)
+ parser.add_argument("--vars",
+ metavar="KEY=VALUE",
+ nargs='?',
+ help="Set a number of key-value pairs "
+ "(do not put spaces before or after the = sign). "
+ "If a value contains spaces, you should define "
+ "it with double quotes: "
+ 'foo="this is a sentence". Note that '
+ "values are always treated as strings.")
+ args = parser.parse_args()
+ if args.config is not None:
+ try:
+ config = json.load(args.config)
+ except Exception as e:
+ print(e)
+ # Installation can't be silent if config is not passed
+ config["silent"] = args.silent
+ if args.vars is not None:
+ try:
+ for var in args.vars.split(' '):
+ key, val = var.split("=")
+ config[key] = val
+ except Exception as e:
+ print(e)
+ config["script"] = args.script
+ return config
+
+
+arguments = initialize_arguments()
# TODO: Learn the dark arts of argparse... (I summon thee dark spawn of cPython)
@@ -46,12 +72,8 @@ def run_as_a_module():
# Add another path for finding profiles, so that list_profiles() in Script() can find guided.py, unattended.py etc.
storage['PROFILE_PATH'].append(os.path.abspath(f'{os.path.dirname(__file__)}/examples'))
-
- if len(sys.argv) == 1:
- sys.argv.append('guided')
-
try:
- script = Script(sys.argv[1])
+ script = Script(arguments.get('script', None))
except ProfileNotFound as err:
print(f"Couldn't find file: {err}")
sys.exit(1)
diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py
index 44f2742b..8f67111a 100644
--- a/archinstall/lib/disk.py
+++ b/archinstall/lib/disk.py
@@ -266,7 +266,7 @@ class Partition:
files = len(glob.glob(f"{temporary_mountpoint}/*"))
iterations = 0
- while SysCommand(f"/usr/bin/umount -R {temporary_mountpoint}").exit_code != 0 and (iterations := iterations+1) < 10:
+ while SysCommand(f"/usr/bin/umount -R {temporary_mountpoint}").exit_code != 0 and (iterations := iterations + 1) < 10:
time.sleep(1)
temporary_path.rmdir()
diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py
index 65c83484..cec3891a 100644
--- a/archinstall/lib/general.py
+++ b/archinstall/lib/general.py
@@ -360,7 +360,8 @@ def prerequisite_check():
def reboot():
o = b''.join(SysCommand("/usr/bin/reboot"))
-def pid_exists(pid :int):
+
+def pid_exists(pid: int):
try:
return any(subprocess.check_output(['/usr/bin/ps', '--no-headers', '-o', 'pid', '-p', str(pid)]).strip())
except subprocess.CalledProcessError:
diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py
index fdeefb84..eb11a47e 100644
--- a/archinstall/lib/networking.py
+++ b/archinstall/lib/networking.py
@@ -1,14 +1,14 @@
import fcntl
-import os
import logging
+import os
import socket
import struct
from collections import OrderedDict
from .exceptions import *
from .general import SysCommand
-from .storage import storage
from .output import log
+from .storage import storage
def get_hw_addr(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
diff --git a/archinstall/lib/systemd.py b/archinstall/lib/systemd.py
index e64ff7e0..383f1f17 100644
--- a/archinstall/lib/systemd.py
+++ b/archinstall/lib/systemd.py
@@ -5,6 +5,7 @@ from .installer import Installer
from .output import log
from .storage import storage
+
class Ini:
def __init__(self, *args, **kwargs):
"""
@@ -103,7 +104,7 @@ class Boot:
return self.session.is_alive()
- def SysCommand(self, cmd :list, *args, **kwargs):
+ def SysCommand(self, cmd: list, *args, **kwargs):
if cmd[0][0] != '/' and cmd[0][:2] != './':
# This check is also done in SysCommand & SysCommandWorker.
# However, that check is done for `machinectl` and not for our chroot command.
@@ -113,8 +114,8 @@ class Boot:
return SysCommand(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs)
- def SysCommandWorker(self, cmd :list, *args, **kwargs):
+ def SysCommandWorker(self, cmd: list, *args, **kwargs):
if cmd[0][0] != '/' and cmd[0][:2] != './':
cmd[0] = locate_binary(cmd[0])
-
- return SysCommandWorker(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs) \ No newline at end of file
+
+ return SysCommandWorker(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs)