Import site theme via console

I am looking for a way to import my site theme (.dcstyle.json file) via the command line so that the configuration can be applied automatically when we run docker.

The theme is stored in the site backup file, so there is no need to do this unless your intent is to be able to create a brand new Discourse with that theme. And if that’s the case, it’s probably easier to just restore a particular backup on those new sites.

You can restore from a backup like this:

cd /var/discourse
./launcher enter app
rails c
SiteSetting.allow_restore=true
exit
discourse restore BACKUPFILENAME

The problem with this approach is that all the posts and users get transferred as well which is what I don’t want
The thing is that we made a test production environment to import our 65k users and 80k posts to discourse. We were using a custom forum and so we had to make our own importer. We got frustrated with the import process taking more than a week, so we decided to limit the number of users to those who have actually had some forum data and manage the rest using SSO.
Now that the script was ready we need to deploy it.
We effectively manage our servers using puppet so I made a puppet script to install discourse. All I need is to automatically set it up
I want to import afresh and so I need to import the theme separately
Therefore I want to include a Ruby script to be run after install. The script currently adds the SSO config and stuff but the theme needs to be done via the website
I wanted to import theme in the same script

Hmm. Surprising that 80k posts would take a week. I’d think much less than a day. Did you base your script on one of the existing ones?

What I generally do in situations as I understand yours, is to do those changes before running the importer. It seems like we’ve already spent more time on this than a it’ll save, but the I’m no stranger to desiring such solutions.

I think we’re drifting away a bit from the original question…

filename = '/tmp/theme.dcstyle.json'
File.open(filename) do |file|
  json = JSON::parse(file.read)
  theme = json['theme']
  puts theme
  t = Theme.new(name: theme["name"], user_id: -1)
  theme["theme_fields"]&.each do |field|
     t.set_field(target: field["target"], name: field["name"], value: field["value"])
  end
  t.save
end
6 Likes

You might want to have a look at the Discourse Theme CLI :wink:

2 Likes