Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated setup: cherrymusic [--conf KEY=VALUE [KEY=VALUE ...]] #665

Open
EWouters opened this issue Mar 11, 2017 · 7 comments
Open

Automated setup: cherrymusic [--conf KEY=VALUE [KEY=VALUE ...]] #665

EWouters opened this issue Mar 11, 2017 · 7 comments

Comments

@EWouters
Copy link

Hi, i installed CherryMusic on my raspberry pi (raspbian) and it works great!

However, I am looking for the documentation on how to automate the setup. I want to find out what arguments I can set with python /opt/cherrymusic/cherrymusic [--conf KEY=VALUE [KEY=VALUE ...]]

I tried the documentation here, but I can't get it to work.

I could of course just edit the configuration file, but the build in method to set the config values seems more appealing.

I tried:

python /opt/cherrymusic/cherrymusic --conf \
  BASEDIR=/music

without succes. Can anyone point me in the right direction?
Thanks for the great work!

@devsnd
Copy link
Owner

devsnd commented Mar 12, 2017

Hey EWouters,

you also have to include the section for the config key, separated by a dot.

Please try to use --conf media.basedir=/music

I've updated the wiki to document this switch (see command line override):
https://github.com/devsnd/cherrymusic/wiki/Setup-guide#user-content-manual-setup

I'll close the issue now, feel free to reopen it, if this does not work as expected.
Have a nice day! 🍰

@devsnd devsnd closed this as completed Mar 12, 2017
@EWouters
Copy link
Author

EWouters commented Mar 16, 2017

Hey, thanks for clarifying!

However, it does not seem to work as expected. I tried this:

root@raspberrypi:/# CHERRY_USER=pi
root@raspberrypi:/# CHERRY_PORT=7600
root@raspberrypi:/# sudo -u $CHERRY_USER python /opt/cherrymusic/cherrymusic --conf \
>       media.basedir=/home/$CHERRY_USER/Music \
>   server.port=$CHERRY_PORT \
>   server.rootpath=/cherrymusic

[170316-15:09] Default configuration file written to '/home/pi/.config/cherrymusic/cherrymusic.conf'

==========================================================================
Welcome to CherryMusic 0.39.1!

To get this party started, you need to edit the configuration file, which
resides under the following path:

    /home/pi/.config/cherrymusic/cherrymusic.conf

Then you can start the server and listen to whatever you like.
Have fun!
==========================================================================

root@raspberrypi:/# head -12 /home/$CHERRY_USER/.config/cherrymusic/cherrymusic.conf

[media]

; BASEDIR specifies where the media that should be served is located. It must be
; an absolute path, e.g. BASEDIR=/absolute/path/to/media.
;
; Links: If your operating system supports them, you can use symlinks directly in
; BASEDIR. Links to directories which contain BASEDIR will be ignored, just like
; all links not directly in, but in sublevels of BASEDIR. This is to guard against
; the adverse effects of link cycles.
;
basedir = None

So it says everything is ok, but the settings do not stick for some reason. Is this a bug?

@EWouters
Copy link
Author

EWouters commented Mar 16, 2017

Perhaps there is a problem here because there is no config file when I call the function?

edit: never mind, also when I call the command twice it doesn't work.

@devsnd
Copy link
Owner

devsnd commented Mar 19, 2017

Yeah, you are right, it seems you cannot run CM without a config file (even an empty file) right now.

You could create a pull request that does not only check if the config file exists, but also allows to pass this test if the media.basedir is set on the command line.

@devsnd devsnd reopened this Mar 19, 2017
@tilboerner
Copy link
Collaborator

I could of course just edit the configuration file, but the build in method to set the config values seems more appealing. [EWouters]

The thing is, --conf is not intended to permanently change the configuation value(s), it's a this-runtime-only override.

You could create a pull request that does not only check if the config file exists, but also allows to pass this test if the media.basedir is set on the command line. [devsnd]

A good solution would be to respect the command line setting for media.basedir (or any other settings) when the config file gets auto-created on first run, like I think it does with the --port. Come to think of it, this is probably exactly what @EWouters expected to happen.

@EWouters
Copy link
Author

EWouters commented Apr 10, 2018

@tilboerner Correct, that is what I was expecting.

So I'm reinstalling my server and I'm tackling the same problem again. Do you recommend me to write a script that I can pass the configuration value(s) to which will edit them in the configuration file or would it be feasible to make a pull request to add a --update-conf KEY=VALUE [KEY=VALUE ...]] argument to the cherrymusic command which will update any configuration value(s) in the (existing) configuration file?

Possible changes here, using this function? It seems this only needs to be a few lines of code. Am I very wrong here?

@tilboerner
Copy link
Collaborator

@EWouters I wouldn't write code to edit the config file. Editing is non-trivial, because users can add their own comments to it, and back when all this code got written, there was no simple way to update the configuration file with configparser without losing those comments. For all I know, this might still be the case. Anyway, that's the reason why cherrymusic only ever writes to this file when it initially creates it.

Luckily, if I understand you correctly, this is exactly the moment you want to influence. You want to write the right configuration when the config file gets created. That should be doable. The files gets created using the configuration defaults. You can change that to also take into account the --conf overrides from the command line. Here's some hints. (Hope you don't mind all the weird, old code. We were still figuring things out back then. 👻)

The whole things happens in the main script:

cherrymusic/cherrymusic

Lines 200 to 204 in 911b757

if not cherrymusicserver.pathprovider.configurationFileExists():
filepath = cherrymusicserver.pathprovider.configurationFile()
cherrymusicserver.create_default_config_file(filepath)
print(welcomeMessage)
sys.exit(0)

The command line config options are in ConfigOptions.configdict at this point. You could pass them to create_default_config_file as a parameter, and in that function merge that with the defaults before writing out the file. The browsersetup handler does something very similar:

def saveconfig(self, values):
collect_errors = cfg.error_collector()
baseconfig = cfg.from_defaults()
newconfig = json.loads(values, encoding='str')
customcfg = baseconfig.replace(newconfig, collect_errors)
if collect_errors:
badkeys = (e.key for e in collect_errors)
return json.dumps({"status": "error", 'fields': list(badkeys)})
cfg.write_to_file(customcfg, pathprovider.configurationFile())

Don't mind the collect_errors stuff, if you leave that out, merge errors will just get raised as exceptions.

You're right, it looks pretty straightforward. I'd be happy to receive a pull request, if you feel up to it. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants