Bash on VNC session – Tabs do not auto complete

Problem : Pressing Tab keys not auto-completing when using Bash within a VNC session (to guest VM running on Ubuntu and XFCE.)

Reason : Unknown, possibly tabs are caught and consumed by the window switch handler.

Fix:

  1. vi ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
  2. search for
    • <property name=”&lt;Super&gt;Tab” type=”string” value=”switch_window_key”/>
  3. Change the above line to –
    • <property name=”&lt;Super&gt;Tab” type=”empty”/>
  4. Reboot the machine.

Reference : https://ubuntuforums.org/archive/index.php/t-1771058.html

Fix: Unable to connect to RabbitMQ server from remote clients

Problem : When connected from a remote client, this error is thrown (at client) – An unexpected connection driver error occured (Exception message: Socket closed)

Reason : As per default settings, the default user (“guest”) can access only from localhost (or Loop back IP).

Fix: Remove “guest” user from “loopback_users”. This is how –

sudo vi /etc/rabbitmq/rabbitmq.config # create new file if in Ubuntu
[{rabbit, [{loopback_users, []}]}].   # Add this line, save and quit vim.

sudo rabbit-server restart # this mostly fails. So we grep & kill in next steps.

ps auxx|grep rabbitmq # gives multiple processes

kill -9 3099 3288 3289 10263 # process-Ids are place holders

sudo rabbitmq-server start

Should be able to connect from remote clients now.

Increase Zuul timeouts for backend BOSH or Comet servers

This is how I could make Spring Zuul work with my backend XMPP (over BOSH) server which had a hanging post of 5 mins.
Please find the relevant settings in application.yml here –

 

zuul:
  add-host-header: true
  host:
   connect-timeout-millis: 5000
   socket-timeout-millis: 601000

hystrix:
 command:
   default:
     execution:
       isolation:
         thread:
           timeoutInMilliseconds: 601000
   xmppServer:
     execution:
       timeout:
         enabled: false;

ribbon:
 eureka:
   enabled: false
xmppServer:
 ribbon:
   ReadTimeout: 601000
   ConnectTimeout: 601000

MySQL Data migration from one server box to another

 

Here’s a quick way to move data from one MySQL server to another

Let source box be ServerOne, and the target box be ServerTwo. (user-names and schema names are indicative) –

  1. On ServerOne,
    1. mysqldump -u username -p db_name > data-dump.sql  (provide password when asked)
    2. scp data-dump.sql serverTwoUsername@ServerTwo:~/
  2. On ServerTwo
    1. mysql -u username -p
    2. create schema schema-name;
    3. exit
    4. mysql -u username -p -h localhost schema-name < data-dump.sql
    5. verify
      1. mysql -u username -p
      2. use schema-name;
      3. show tables;

Should be done.

Fix : Cassandra not accessible from outside the local machine.

Problem: In Single node setup of cassandra, after installation and starting Cassandra, the native port (9042) is not accessible from outside the local box.

Reason : By default Cassandra binds to the loopback IP or localhost.

Fix:


sudo vi /etc/cassandra/cassandra.yaml

# Comment out below line
#listen_address: localhost

# Uncomment below lines
listen_interface: eth0
listen_interface_prefer_ipv6: false

#Comment out below line
# rpc_address: localhost

#Uncomment below lines
rpc_interface: eth0
rpc_interface_prefer_ipv6: false

seeds: "127.0.0.1,192.168.14.49" #Added the LAN address after a comma


Install Artefactory Maven repository on Ubuntu 16.04

 

Follow these steps to install Artifactory Maven repository on Ubuntu 16.04 ( terminal commands, mostly )-

  1. wget -c -O- “https://bintray.com/user/downloadSubjectPublicKey?username=jfrog” | sudo apt-key add –
  2. echo “deb https://bintray.com/artifact/download/jfrog/artifactory-debs xenial main” | sudo tee -a /etc/apt/sources.list.d/artifactory-oss.list
  3. sudo apt-get update
  4. sudo apt-get -y install jfrog-artifactory-oss
  5. Set credentials of DB accounts after running –
    1. sudo /opt/jfrog/artifactory/bin/configure.mysql.sh

Refer : https://hostpresto.com/community/tutorials/how-to-install-jfrog-artifactory-on-ubuntu-14-04/

After installation, launch the Artefactory portal. Then –

  1. From Security Configuration page, remove Anonymous access (untick the checkbox).
  2. Also scroll down and activate password Encryption.
  3. Create a new user (Eg, “developer”), as the existing user is Admin, and granular permissions cannot be added to it. Also for some reason, unless we add permissions for Manage, Overwrite, Read etc individually, the Artefactory would continue to respond with 401 for any request.
  4. Logout Admin, and login as the user created in above step.
  5. Go to user profile by click on users name on top right corner, and unlock the page by entering the user’s password again.
  6. Scroll down and view the encrypted password by click on the “eye” icon adjacent to it. Note this down.
  7. From Home page, click on repository “libs release”. Unlock credentials in this panel by entering the user’s  password again.
  8. Click and “Generate Maven Settings”, and download the maven settings file.
  9. Edit Maven Settings file –
    1. Change parameterized username to username of the user (eg, developer)
    2. Change parameterized password to just the encrypted password (obtained at step 6)
    3. Save the file, and move it to /[user-home]/.m2/settings.xml
  10. Test the above setup by running “mvn deploy” on any of the maven project you have.

Spring boot and Open UI5 error : JSON.parse: unexpected end of data – when setting resourceroots

Problem : After setting the tag data-sap-ui-resourceroots the UI5 loading stops with the error “JSON.parse: unexpected end of data …”

Reason : Spring boot thymeleaf view formatter changes single quotes to double quotes, thus braking the valid JSON format of the tag.

    <script src="/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_belize" data-sap-ui-libs="sap.m" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async" data-sap-ui-resourceroots="{"demo":"uidemo"}">
<!-- note the stupid double quotes in the json at the end of above line -->
<!-- this was originally single quotes - '{"demo":"uidemo"}' -->

Fix :  

  • Make the landing page a static HTML instead of a view template (move from templates folder to static, in Spring boot).
  • OR
  • Move to Velocity or JSP formatters

Spring boot : Testing Cassandra Repositories using Cassandra Unit

 

This short tutorial is about getting test classes running for the Cassandra repositories in a Spring boot application. The example provided uses Maven, Spring-data-cassandra and Cassandra-Unit. It also uses Lombok plugin, which is why you won’t see any getters/setters in these beans.

Step 1 : Add dependencies

Add these dependencies in your POM file –

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-cassandra</artifactId>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.cassandraunit</groupId>
            <artifactId>cassandra-unit-spring</artifactId>
            <version>3.1.1.0</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.cassandraunit</groupId>
                    <artifactId>cassandra-unit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.cassandraunit</groupId>
            <artifactId>cassandra-unit</artifactId>
            <classifier>shaded</classifier>
            <version>3.1.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.cassandraunit</groupId>
            <artifactId>cassandra-unit-spring</artifactId>
            <version>3.1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.hectorclient</groupId>
            <artifactId>hector-core</artifactId>
            <version>2.0-0</version>
            <scope>test</scope>
        </dependency>

Step 2 : Deciding on alternate port for test Cassandra and creating a corresponding YAML file.

  1. Download this file : another-cassandra.yaml
  2. Edit the property “native_transport_port” to the port you need the test (embedded) Cassandra to listen to. The port configured in the file is 9152, which can be used unchanged.
  3. Add the file to class-path (resources folder).

Step 3 : Write a simple test to check if the embedded Cassandra starts up fine.

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;

import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;

import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;


public class EmbededCassandraTest {
    @Before
    public void before() throws TTransportException, IOException, InterruptedException, ConfigurationException {
        EmbeddedCassandraServerHelper.startEmbeddedCassandra("another-cassandra.yaml", 20000);
    }

    @Test
    public void shouldHaveAnEmbeddedCassandraStartOn9175Port() throws Exception {
        Cluster cluster = HFactory.getOrCreateCluster("TestCluster", new CassandraHostConfigurator("localhost:9175"));
        assertThat(cluster.getConnectionManager().getActivePools().size(), is(1));
        KeyspaceDefinition keyspaceDefinition = cluster.describeKeyspace("system");
        assertThat(keyspaceDefinition, notNullValue());
        assertThat(keyspaceDefinition.getReplicationFactor(), is(1));

    }
    @AfterClass
    public static void cleanCassandra() {
        EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
    }
}

Step 4: Create or Edit the application-test.properties for setting the embedded cassandra server settings for the test profile.

Add these properties to application-test.properties. Note that these properties would be picked by the config file we will add in steps further down.

cassandra.hosts=localhost
cassandra.port=9152
cassandra.keyspace=yourkeyspace

Step 5: Spring boot Java Config files

These are the main and test Cassandra config classes for this example –

Main Cassandra Config:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;


@Configuration
@EnableCassandraRepositories("package.of.your.repositories")
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Value("${cassandra.hosts}")
    String cassandraHosts;
    @Value("${cassandra.port}")
    String cassandraPort;
    @Value("${cassandra.keyspace}")
    String cassandraKeySpace;

    @Override
    public String getKeyspaceName() {
        return cassandraKeySpace;
    }

    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster =
                new CassandraClusterFactoryBean();
        cluster.setContactPoints(cassandraHosts);
        cluster.setPort(Integer.parseInt(cassandraPort));
        return cluster;
    }


    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }
    @Override
    public String[] getEntityBasePackages() {
        return new String[]{
                "package.of.your.domains"};
    }
}

Test Cassandra Config :

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ActiveProfiles;


@Configuration
@ActiveProfiles("test")
@PropertySource( {"classpath:application.properties",
        "classpath:application-test.properties"})
public class CassandraTestConfig extends CassandraConfig {

}

Step 6 : Write the abstract test class

Write this abstract class which the repository test classes can inherit, and which will abstract out the Cassandra configurations :


import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.mycompany.config.CassandraTestConfig;
import lombok.extern.slf4j.XSlf4j;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.context.annotation.Import;

import java.io.IOException;
import java.util.Properties;


@Import(CassandraTestConfig.class)
@XSlf4j
public abstract class AbstractRepositoryTest {

    @BeforeClass
    public static void initCassandra() {
        try {
            Properties prop = new Properties();
            prop.load(AbstractRepositoryTest.class.getClassLoader().getResourceAsStream("application-test.properties"));
            String cassandraHosts = prop.getProperty("cassandra.hosts");
            String cassandraPort = prop.getProperty("cassandra.port");

            EmbeddedCassandraServerHelper.startEmbeddedCassandra("another-cassandra.yaml", 20000);
            log.info("Connect to embedded db");
            Cluster cluster = Cluster.builder().addContactPoints(cassandraHosts).withPort(Integer.parseInt(cassandraPort)).build();
            Session session = cluster.connect();

            log.info("Initialize keyspace");
            session.execute("create keyspace  \"mykeyspace\" WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};");
            session.execute("use \"mykeyspace\";");
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Before
    public void initTest() {
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @AfterClass
    public static void cleanCassandra() {
        EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
    }

}

Step 7 :  Domain Object/Entity

import com.datastax.driver.core.utils.UUIDs;
import lombok.Data;
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;

import java.io.Serializable;
import java.util.UUID;


@Data
@Table(value = "sampleEntity")
public class SampleEntity implements Serializable {
    @PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.PARTITIONED, ordering = Ordering.DESCENDING)
    private UUID id;
    private String text;
}

Step 8 :  Repository for ze entity


import org.springframework.data.cassandra.repository.CassandraRepository;

import java.util.List;
import java.util.UUID;

public interface SampleEntityRepository extends CassandraRepository<SampleEntity> {
    SampleEntity findById(UUID id);
}

Step 9 : The repository test, gentlemen !


import com.datastax.driver.core.utils.UUIDs;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.UUID;


@RunWith(SpringJUnit4ClassRunner.class)
public class SampleEntityRepoTest extends AbstractRepositoryTest {

    @Autowired
    SampleEntityRepository sampleEntityRepository;

    @Test
    public void saveOneTest() {
        UUID id = UUIDs.timeBased();
        SampleEntity sampleEntity = new SampleEntity();
        sampleEntity.setId(id);
        sampleEntity.setText("Hi");
        sampleEntity = sampleEntityRepository.save(sampleEntity);
        SampleEntity retrievedSampleEntity = sampleEntityRepository.findById(id);
        Assert.assertEquals("Hi", sampleEntity.getText());
    }
    //other tests...
}

Done.
Run the test class to confirm the setup works (or not – which case you are on your own).