Permalink
Browse files

Generate locales as part of bootstrap

  • Loading branch information...
1 parent d6604d8 commit 84d425a362c01d51f5ea876993b82d7d819c0564 @justinsb justinsb committed May 16, 2012
@@ -0,0 +1,23 @@
+package org.platformlayer.ops.bootstrap;
+
+import org.apache.log4j.Logger;
+import org.platformlayer.ops.Handler;
+import org.platformlayer.ops.OpsException;
+import org.platformlayer.ops.filesystem.SimpleFile;
+import org.platformlayer.ops.tree.OpsTreeBase;
+
+public class BootstrapLocales extends OpsTreeBase {
+ @SuppressWarnings("unused")
+ private static final Logger log = Logger.getLogger(BootstrapLocales.class);
+
+ @Handler
+ public void handler() {
+
+ }
+
+ @Override
+ protected void addChildren() throws OpsException {
+ addChild(SimpleFile.build(getClass(), RegenerateLocales.LOCALE_GEN_FILE));
+ addChild(RegenerateLocales.class);
+ }
+}
@@ -8,6 +8,7 @@
import org.platformlayer.ops.tree.OpsTreeBase;
public class InstanceBootstrap extends OpsTreeBase {
+ @SuppressWarnings("unused")
private static final Logger log = Logger.getLogger(InstanceBootstrap.class);
@Handler
@@ -18,6 +19,8 @@ public void handler() {
protected void addChildren() throws OpsException {
addChild(PersistIptablesScripts.class);
+ addChild(BootstrapLocales.class);
+
// We always install curl, because we use it to check for http proxy responsiveness
// TODO: Switch to netcat, to avoid using curl here - it's quite big
addChild(PackageDependency.build("curl"));
@@ -0,0 +1,95 @@
+package org.platformlayer.ops.bootstrap;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.platformlayer.ops.Command;
+import org.platformlayer.ops.Handler;
+import org.platformlayer.ops.OpsContext;
+import org.platformlayer.ops.OpsException;
+import org.platformlayer.ops.OpsTarget;
+import org.platformlayer.ops.process.ProcessExecution;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.Lists;
+
+/**
+ * Configures locales
+ *
+ * Thanks to: http://people.debian.org/~schultmc/locales.html
+ *
+ * @author justinsb
+ *
+ */
+public class RegenerateLocales {
+ private static final Logger log = Logger.getLogger(RegenerateLocales.class);
+
+ public static final File LOCALE_GEN_FILE = new File("/etc/locale.gen");
+
+ @Handler
+ public void handler(OpsTarget target) throws OpsException {
+ List<String> actual = getGeneratedLocales(target);
+ List<String> requested = getRequestedLocales(target);
+
+ boolean missing = false;
+
+ for (String locale : requested) {
+ if (!actual.contains(locale)) {
+ log.info("Detected missing locale: " + locale);
+ missing = true;
+ }
+ }
+
+ if (missing && OpsContext.isConfigure()) {
+ log.info("Regenerating locales");
+ target.executeCommand(Command.build("/usr/sbin/locale-gen"));
+ }
+ }
+
+ private List<String> getRequestedLocales(OpsTarget target) throws OpsException {
+ String localeGen = target.readTextFile(LOCALE_GEN_FILE);
+ if (localeGen == null) {
+ throw new OpsException("/etc/locale.gen not configured");
+ }
+
+ List<String> locales = Lists.newArrayList();
+ for (String line : Splitter.on("\n").split(localeGen)) {
+ line = line.trim();
+ if (line.isEmpty()) {
+ continue;
+ }
+ if (line.startsWith("#")) {
+ continue;
+ }
+
+ // en_US.UTF-8 UTF-8 -> en_US.UTF-8
+ if (line.contains(" ")) {
+ line = line.substring(0, line.indexOf(' '));
+ }
+ locales.add(normalize(line));
+ }
+ return locales;
+ }
+
+ private List<String> getGeneratedLocales(OpsTarget target) throws OpsException {
+ Command listLocales = Command.build("localedef --list-archive");
+ ProcessExecution execution = target.executeCommand(listLocales);
+
+ List<String> locales = Lists.newArrayList();
+ for (String line : Splitter.on("\n").split(execution.getStdOut())) {
+ line = line.trim();
+ if (line.isEmpty()) {
+ continue;
+ }
+ locales.add(normalize(line));
+ }
+ return locales;
+ }
+
+ private String normalize(String s) {
+ s = s.toLowerCase();
+ s = s.replace(".utf-8", ".utf8");
+ return s;
+ }
+}
@@ -0,0 +1,8 @@
+# Generated by PlatformLayer
+
+# This file lists locales that you wish to have built. You can find a list
+# of valid supported locales at /usr/share/i18n/SUPPORTED, and you can add
+# user defined locales to /usr/local/share/i18n/SUPPORTED. If you change
+# this file, you need to rerun locale-gen.
+
+en_US.UTF-8 UTF-8

0 comments on commit 84d425a

Please sign in to comment.